Problem with date() and strtotime
Here is what I hav开发者_如何学运维e:
$str = '12-25-2009';
echo date('Y-m-d', strtotime($str));
This produces 1969-12-31 instead of 2009-12-25. If I set the $str var to 01-01-2009, I will get 2009-01-01 which is correct. Why is this happening?
Try $str = '25-12-2009';
or $str = '12/25/2009';
if the format of date is day month year use dash (-) else if the format was month day year use slashes (/)
Using dashes as a delimiter, it's parsing as a european date, aka DD-MM-YYYY. 01-01-2009 is a valid date in either MM/DD/YYYY or DD-MM-YYYY but 12-25-2009 is only valid as MM-DD-YYYY which isn't really correct. The correct representation for MM/DD/YYYY is with slashes, which another poster has already indicated does indeed parse correctly.
If you use YYYY-MM-DD (ISO date format) for everything you'll be able to avoid at least some of these frustrations.
strtotime('12-25-2009')
returns false
, which is then casted into an integer by date
.
It seems that using dashes confuses strtotime
. The same date, with slashes instead, works like a charm:
echo strtotime('12/25/2009'); // yields 1261717200
精彩评论