Is a date in a given range using PHP

To find if a date is in a given data range, the quikest way is to turn all dates to Epoch dates and then do a compare.

So to test if 31/12/2007 is in a date range of 10-10-2000 and 10-10-2008 do the following

$From=”31-12-2007″;
$To=”10-10-2008″;
$Checkdate=”01-01-2008″;

if (
( mktime(0,0,0, date(”m”,strtotime($From)),date(”d”,strtotime($From)),date(”Y”,strtotime($From)))
<=
mktime(0,0,0,date(”m”,strtotime($Checkdate)),date(”d”,strtotime($Checkdate)),date(”Y”,strtotime($Checkdate))) )

&&

(mktime(0,0,0,date(”m”,strtotime($To)),date(”d”,strtotime($To)),date(”Y”,strtotime($To)))
>=
mktime(0,0,0,date(”m”,strtotime($Checkdate)),date(”d”,strtotime($Checkdate)),date(”Y”,strtotime($Checkdate))) ) ) {

print “In Date Range”;

} else {

print “Not in Range”;
}

Hope this helps.

Leave a Reply