简体   繁体   中英

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. 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. 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. So you have to verify if $resu == 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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