Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strtotime returning false

Tags:

php

I have datestrings that looks like this:

"2012 28 Nov 21:00 CET"

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

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

like image 976
Johan Avatar asked Nov 28 '12 21:11

Johan


People also ask

Why is Strtotime return false?

If the date string isn't understood by strtotime() then it will return false. When this happens you can try a few things to force strtotime() to parse the date correctly. Sometimes it's something as simple as swapping the slashes for dashes, which forces strtotime() to parse the date in a different way.

What does Strtotime return?

The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.

Which is a valid Strtotime () function in PHP?

The strtotime() function is a built-in function in PHP which is used to convert an English textual date-time description to a UNIX timestamp. The function accepts a string parameter in English which represents the description of date-time. For e.g., “now” refers to the current date in English date-time description.

How do I use Strtotime?

If you want to use the PHP function strtotime to add or subtract a number of days, weeks, months or years from a date other than the current time, you can do it by passing the second optional parameter to the strtotime() function, or by adding it into the string which defines the time to parse.


2 Answers

2012 28 Nov 21:00 CET is weird date/time format. Y-D-M? Where are you getting that?

At any rate, the DateTime object has a method createFromFormat that will do a better job parsing that:

$dt = DateTime::createFromFormat("Y d M H: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 a "English textual datetime" (according to the manual), which Y-D-M is not. Any time strtotime returns false, it simply doesn't understand your string, which in this application is expected. A note on the manual page deals with this issue:

Note:

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

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

That's just as well, DateTime is a superior tool for any interaction with dates or times.

Documentation

  • DateTime class- http://www.php.net/manual/en/class.datetime.php
  • DateTime::createFromFormat - http://www.php.net/manual/en/datetime.createfromformat.php
  • strtotime - http://php.net/manual/en/function.strtotime.php
  • date (also lists date format strings) - http://php.net/manual/en/function.date.php
like image 124
Chris Baker Avatar answered Sep 28 '22 02:09

Chris Baker


strtotime doesn't create a date object, it simply tries to parse your input and returns a unix timestamp.

You can find valid formats here: http://www.php.net/manual/en/datetime.formats.php

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

would output 1354132800

to create a php DateTime object you could do this:

$date = new DateTime('2012-11-28T21:00:00+01:00');
echo $date->format('Y-m-d H:i:s');
like image 20
Michel Feldheim Avatar answered Sep 28 '22 01:09

Michel Feldheim