Comparing two ISO8601 dates strings in PHP

Posted by oompahloompah on Stack Overflow See other posts from Stack Overflow or by oompahloompah
Published on 2011-02-23T22:47:02Z Indexed on 2011/02/23 23:25 UTC
Read the original article Hit count: 236

Filed under:

I need to compare (actually rank/sort) dates in a PHP script. The dates are ISO-8601 Date format i.e.

YYYY-MM-DD

I wrote a comparison function which splits the dates and compares by year/month/day. However, it seems this may be overkill and I could just as easily done a simple string comparison like:

if ($date1 < $date2)
   // do something
elseif( $date1 > $date2)
   //do something else
else
   //do yet another thing

Is my assumption about (ISO-8601) Date string comparison correct - i.e. can I get rid of my function (to save a few clock cycles on the server), or is it safer to explicity do the comparison in a custom function?

© Stack Overflow or respective owner

Related posts about php