2

I want to check the today's date with specified range of dates and want it to return true if the today's date is between the specified range dates

Something like this:

if (todaysDate is between "2017-04-24 ... 2017-08-30") {
    return true;
}

Is there any way to check this in PHP?

Omer
  • 1,727
  • 5
  • 28
  • 47

2 Answers2

1

Here is some code that may help you, create a function if you want to.

$today= date('Y-m-d');
$today=date('Y-m-d', strtotime($today));;
$date1= date('Y-m-d', strtotime("01/01/2001"));
$date2= date('Y-m-d', strtotime("01/01/2012"));

if (($today> $date1) && ($today< $date2)){
  echo "OK !";
}else{
  echo "NO OK !";  
}
K.Fanedoul
  • 281
  • 2
  • 18
1

Just create your own method like so:

function isBetweenDates($dateToCheck, $firstDate, $secondDate){
    if (($dateToCheck > $firstDate) && ($dateToCheck < 
        $secondDate))
    {
      return true;
    }
    else
    {
      return false;
    }
}

Then call it with dates:

echo isBetweenDates(date('Y-m-d'),strtotime("01/01/2016"),strtotime("01/01/2018"));

Which will return true, because today's date is between 2016 and 2018.

based on: PHP check if date between two dates

Edit:

You could even generalize the function and use it on ints too:

function isBetween($varToCheck, $lowerLimit, $upperLimit){
        if (($varToCheck > $lowerLimit) && ($varToCheck <  
            $upperLimit))
        {
          return true;
        }
        else
        {
          return false;
        }
    }

Or even make it super specific by converting the input to dates:

function isBetweenDates($dateToCheck, $start_date, $end_date)
{
  $start = strtotime($start_date);
  $end = strtotime($end_date);
  $date = strtotime($dateToCheck);

  // Check that user date is between start & end
  return (($date > $start) && ($date < $end));
}
Rick van Lieshout
  • 2,276
  • 2
  • 22
  • 39