[thelist] simple question about comparing dates in PHP/MySQL

Lee Kowalkowski lee.kowalkowski at googlemail.com
Tue Jul 7 14:35:45 CDT 2009

2009/7/7 Nan Harbison <nan at nanharbison.com>:
> This might be a stupid question, but someone was trying to tell me that you
> can compare dates in the format:
> 2009-06-04 > 2009-05-30.
> I thought I had to convert dates using strtotime() to compare dates, but he
> said he had been doing it and it worked fine. I have found it works
> sporadically.
> Can anyone give me a definitive answer on this subject? I googled for it,
> and most articles have you convert the date, but nowhere does any article
> say you actually have to convert or why. I think it is because the date as
> 2009-07-7 is a string and it is doing some string comparison?

To sort your dates it should work fine as long as you are using
yyyy-mm-dd, both strings use the same encoding and you always zero-pad
your single-digit months and days.  It'll work until the year 10000

You can't use this if you're interested in the difference between the
dates, obviously.


More information about the thelist mailing list