Maybe I need more sleep but I have some weirdness with strtotime I cannot explain.
The UK localised notation for a date is d/m/Y which is what I am getting within a CSV from an external source to insert into MySQL (format: Y-m-d).
The immediate problem I saw, of course, was that PHP (when using strtotime) converted these wrong. Below is an example of a var_dump of the field value with the post-strtotime var_dump after:
string(10) "13/07/1992" string(19) "1969-12-31 16:00:00" // This one is odd
string(10) "07/09/1992" string(19) "1992-07-09 00:00:00"
string(10) "09/11/1992" string(19) "1992-09-11 00:00:00"
Originally I thought it was because PHP did not support the UK notation ( http://www.php.net/manual/en/datetime.formats.date.php ) and believes it is an American date however that does not explain why if I change my function to replace / with -:
date('Y-m-d H:i:s', strtotime(str_replace('/', '-', $string_time)))
The same dates do work:
string(10) "13/07/1992" string(19) "1992-07-13 00:00:00"
string(10) "07/09/1992" string(19) "1992-09-07 00:00:00"
string(10) "09/11/1992" string(19) "1992-11-09 00:00:00"
I infact have a test set of 200 dates that all work under the - notation.
Shouldn’t the same rules apply here according to the documentation?
The US format uses slashes because that is the convention over there. Normally, dashes aren’t used.
strototime()‘s behavior is quite simply:A more technical list of formats (and how
strototime()interprets them) can be found here.You might also be interested in DateTime::createFromFormat.