0

如何检查当前日期是否在假期日期范围内?我有一个包含两个 DATE 列 start_date 和 end_date 的“假期”表。用户可以在该范围内定义假期日期。我需要创建一个循环来检查假日日期范围内的当前日期,如果是,则当前日期变为“+1 天”,然后再次检查。到目前为止,我已经做到了:

<?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";
    }
?>
4

4 回答 4

1

试试这个。

<?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));
     }
  }
?>
于 2012-08-30T10:49:23.470 回答
1

这应该有效:

<?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']));
}
于 2012-08-30T10:49:25.983 回答
1

你不需要有一个循环,所以你可以这样做

<?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);
   }
?>
于 2012-08-30T10:53:50.583 回答
0

$resu将包含结果行数。所以你必须验证是否$resu == 0

于 2012-08-30T10:38:01.690 回答