Strtotime returns false

I have datestrings that looks like this:

"2012 28 Nov 21:00 CET" 

strtotime("2012 28 Nov 21:00 CET") returns false.

Is there a way to convert this string to a date object, or do I need to format it differently?

+6
source share
2 answers

2012 28 Nov 21:00 CET is a weird date / time format. U-D-M? Where do you understand that?

In any case, the DateTime object has a createFromFormat method, which will be better createFromFormat in the following:

 $dt = DateTime::createFromFormat("Y d MH:i T", '2012 28 Nov 21:00 CET'); $ts = $dt->getTimestamp(); echo $ts; // 1354132800 

Try it here: http://codepad.viper-7.com/NfAmcw

strtotime expects an "English text datetime" (according to the manual ), which YDM is not. Any time strtotime returns false, it just does not understand your string, which is expected in this application. A note on the manual page addresses this issue:

Note:

Dates in m / d / y or dmy formats are eliminated by looking at the separator between the various components: if the separator is slash (/), then the American m / d / y is assumed; whereas if the separator is a dash (-) or a period (.), then the European dmy format is assumed.

To avoid potential ambiguity, it is best to use ISO 8601 (YYYY-MM-DD) dates or DateTime :: createFromFormat () when possible.

Similarly, DateTime is an excellent tool for any interaction with dates or times.

Documentation

+13
source

strtotime does not create a date object, it just tries to parse your input and returns a unix timestamp.

Here you can find valid formats: http://www.php.net/manual/en/datetime.formats.php

 echo strtotime("2012-11-28T21:00:00+01:00"); 

prints 1354132800

to create a php DateTime object you can do this:

 $date = new DateTime('2012-11-28T21:00:00+01:00'); echo $date->format('Ymd H:i:s'); 
+1
source

All Articles