I’m having trouble with MySQL format_date, and don’t understand why. I have the following as part of my code:
date_format(NOW() + INTERVAL 3 DAY, '%Y-%m-%d')
which seems to work fine, except for the fact that regardless of the date I choose, the %d is returning as a single zero (‘0’). If I change %d to %e I can get the correct date, but I’m using this to compare dates, so I need the leading zero for numbers below 10. Is this a database setting, or am I missing something obvious?
Thanks in advance.
UPDATE: I feel like it has to be something in the db, because when I simplify the query to this:
$q = "SELECT date_format(NOW() + INTERVAL 3 DAY, '%Y-%m-%d') as 'today' from content_field_date LIMIT 1";
‘today’ prints out as ‘2012-03-0’
Meanwhile, this:
$q = "SELECT date_format(NOW() + INTERVAL 3 DAY, '%Y-%m-%e') as 'today' from content_field_date LIMIT 1";
correctly returns ‘2012-03-17’
Zeth
WHat you describe should not happen. You either found a MySQL bug or you are doing something wrong.
If you only want to compare dates, you can do that inside MySQL and you probably shouldn’t be using
DATE_FORMAT()at all. You can use this to get a date:or: