1

How do I go about checking to see if the current date is between two other dates?

I've been trying different operators, but haven't been able to figure this out.

So, if I have a from date of 2/2/2010 and a to date of 2/10/2010, how can I return TRUE if the current date (2/4/2010) falls between those two dates?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
phpN00b
  • 819
  • 3
  • 15
  • 28

5 Answers5

3

Off the top of my head, I don't know of a date comparison operator in PHP, but I would use strtotime() on all three dates, then do simple mathematical comparisons.

<?php

$early_date = strtotime("02/02/2010");
$date = strtotime("02/04/2010");
$late_date = strtotime("02/10/2010");

if (($early_date < $date) && ($date < $late_date)) {
  echo "true";
}

returns true.

Brock Batsell
  • 5,786
  • 1
  • 25
  • 27
1

If past date one and before date two, then it's between them.

Andrew
  • 12,172
  • 16
  • 46
  • 61
1

wouldn't this work ?

 ( ($lowerlimitdate <= $checkingdate) && ($checkingdate <= $upperlimitdate))
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
1

To do a comparison like this you need to do separate comparisons. If $d is the date you want to compare, $d1 is the earlier date, and $d2 is the later date, it would be something like:

if ((strtotime($d) > strtotime($d1)) and (strtotime($d) < strtotime($d2))) {
    return true;
} else {
    return false;
}
GreenMatt
  • 18,244
  • 7
  • 53
  • 79
0

the googles told me

if ( strtotime($date) > strtotime('22/09/2008') && strtotime($date) < strtotime('28/09/2008'))

http://answers.yahoo.com/question/index?qid=20081003113922AAHnQsp

John Boker
  • 82,559
  • 17
  • 97
  • 130