Page 1 of 1

how to compare between two dates

Posted: Wed Jun 02, 2004 2:44 pm
by davidklonski
Hi

I have two date strings thata were generated using:

Code: Select all

$date1= date("Y-m-d");
$date2= date("Y-m-d");
Assuming that I am know looking at $date1 & $date2, how can I compare between them?
That is:
return >0 if $date1 comes before $date2
return <0 if $date1 comes after $date2
return 0 if $date1 is exactly the same date as $date2

thanks in advance

Posted: Wed Jun 02, 2004 2:49 pm
by John Cartwright
When comparing dates you should convert them to time stamps

Posted: Wed Jun 02, 2004 3:38 pm
by pickle
I wholeheartedly agree with ~phenom. Timestamps makes manipulating dates much easier. However, what you want to do should work with a simple if/else if/else if construct

Code: Select all

if($date1 < $date2)
{
   $retval = 1;
}
else if($date1 > $date2)
{
   $retval = -1;
}
else if($date1 === $date2)
{
   $retval = 0;
}

return($retval);