Date comparison

How accurate is it to compare dates like this?

$today = date('Y-m-d'); 
$outdate = "2011-11-02";
if($today >= $outdate) {...

I used to know that it was necessary to convert to different timestamps, but it seems now, if you set the date in this format. everything should be ok, right?

Answer 1, authority 100%

If both dates are exactly in the format Y-m-d(or Y-m-d H:i:s), then it is logical to compare them as strings, i.e.

$result=($date1<$date2); //$result === true

But if you are not sure that the date format is the same, then it is better to convert them to the unixtime format and compare them as numbers:

$result=($date1<$date2); //$result === false
$result=(strtotime($date1)<strtotime($date2)); //$result === true

Answer 2, authority 25%

Use the strtotimefunction, i.e. translate your strings you get via date into a unix timestamp, i.e. to an int number and then it is convenient to compare

Answer 3, authority 8%

It is impossible to compare as date strings in any case, it will not be correct if you have:

$date1 = '01.03.1956';
$date2 = '05.06.1930';

Expression $date2 > $date1 ? true : false;Will return truebecause the strings are compared character by character, respectively because 5 > 1, line $date2 > $date1.

Of course the format 2011-02-12will return a valid comparison, but this is potentially dangerous code.