简体   繁体   English

检查当前日期是否在假期日期范围内

[英]Check if current date falls in holiday date range

How to check if current date falls in holiday date range? 如何查看当前日期是否在假期日期范围内? I have a table "holidays" with two DATE columns, start_date and end_date. 我有一个带有两个DATE列start_date和end_date的表“假期”。 Users can define holidays dates in that range. 用户可以在该范围内定义假期日期。 I need to make a loop that checks is the current date within the holiday date range, and if it is, the current date goes "+1 day", and checks again. 我需要做一个循环,检查假日日期范围内的当前日期,如果是,则当前日期变为“ +1天”,然后再次检查。 I've made it so far: 到目前为止,我已经做到了:

<?php
include ("config.php");
$curdate = date('Y-m-d', time());
$res = mysql_query("SELECT * FROM holidays WHERE '$curdate' BETWEEN `start_date` and `end_date`");
$resu = mysql_num_rows($res);
 if ($resu == NULL)
      {
      echo "Date is not range";
      }
 else
    {
    echo "Date is in range";
    }
?>

Try this one. 试试这个。

<?php
  include ("config.php");
  $curdate = date('Y-m-d', time());

  while(1) {
     $res = mysql_query("SELECT * FROM holidays WHERE '$curdate' BETWEEN `start_date` and `end_date`");
     if(!mysql_num_rows($res))
     {
         echo "Date is not range";
         break;
     }
     else
     {
         echo "Date is in range";
         $TS = strtotime($curdate);
         $curdate = date('Y-m-d', strtotime('+1 day', $TS));
     }
  }
?>

This should work: 这应该工作:

<?php
include ("config.php");

$curdate = date('Y-m-d', time());

while(1)
{
    $res = mysql_query("SELECT * FROM holidays WHERE '$curdate' BETWEEN `start_date` and `end_date` LIMIT 1");

    if( !mysql_num_rows($res) )
    {
        echo 'closest data available: ' . $curdate;
        break;
    }

    $ar = mysql_fetch_assoc($res);
    $curdate = date('Y-m-d', strtotime("+1 day", $ar['end_date']));
}

You don't need to have a loop, so you can do it like this 您不需要循环,因此您可以这样做

<?php
   include ("config.php");
   $curdate = date('Y-m-d', time());
   $res = mysql_query("SELECT id FROM holidays WHERE '$curdate' BETWEEN `start_date` and `end_date`");
   $resu = mysql_num_rows($res);
   if ($resu == 0)
   {
      echo "Date is not range";
   }
   else
   {
      $res = mysql_query("SELECT end_date FROM holidays WHERE end_date > '$curdate' ORDER BY end_date ASC LIMIT 1");
      $resu = mysql_fetch_array($res);

      $next_day = strtotime($resu['end_date']) + 24 * 60 * 60;
      echo 'The next available day is ' . date("Y-m-d", $next_day);
   }
?>

$resu will contain the number of resulted rows. $resu将包含结果行的数量。 So you have to verify if $resu == 0 因此,您必须验证$resu == 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM