Problem with date () and strtotime

Here is what I have:

$str = '12-25-2009'; echo date('Ym-d', strtotime($str)); 

This gives 1969-12-31 instead of 2009-12-25. If I install $ str var before 01-01-2009, I will get 2009-01-01, which is correct. Why is this happening?

+4
source share
3 answers

Try $str = '25-12-2009'; or $str = '12/25/2009';

if the date format is day month year of use dash (-) else, if the format was month in day, use slashes (/)

+2
source

strtotime('12-25-2009') returns false , which is then placed as an integer on date .

Using dashes seems to confuse strtotime . On the same day, with a slash it works like a charm:

 echo strtotime('12/25/2009'); // yields 1261717200 
+2
source

Using a dash as a separator, it is parsed as a European date, aka DD-MM-YYYY. 01-01-2009 is a valid date in MM / DD / YYYY or DD-MM-YYYY, but 12-25-2009 only acts as MM-DD-YYYY, which is actually not true. The correct representation for MM / DD / YYYY is the slashes that another poster has already indicated.

If you use YYYY-MM-DD (ISO date format) for everything, you can avoid at least some of these frustrations.

+2
source

All Articles