Let's start with the right way to do it. If you want to pass a timestamp to strtotime
you have to prefix it with '@'
. It is explained on the Compound Formats page, under "Localized notations" -> "Unix Timestamp".
echo(date('r', strtotime('@1420066800'))."\n");
echo(date('r', strtotime('@1451602800'))."\n");
The output is:
Thu, 01 Jan 2015 01:00:00 +0200
Fri, 01 Jan 2016 01:00:00 +0200
Now, why strtotime()
returns false
for 1420066800
and 26197048320
for 1451602800
?
It expects to receive a string and if it receives a number it doesn't care and converts it to string first. Then it follows some rules and tries to identify the date components into the string. Because neither '1420066800'
nor '1451602800'
contains any separator for components, it probably tries to guess the order of components.
Today, 2016-02-25
, strtotime('1451602800')
produces a timestamp that, converted to a printable date looks like: 'Fri, 25 Feb 2800 14:52:00 +0200'
It makes me think it interprets the input string as follows: 14:51:60
is the time, 2800
is the year, the other components (day, month) are initialized from the current time.
The documentation says:
The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC
), relative to the timestamp given in $now
, or the current time if $now
is not supplied.
Since the "date" you provide doesn't follow any valid date-time format, strtotime()
is free to return any value. It is called "garbage in, garbage out".
false
for me – ButtonballDateTime
class to this purposes, and you don't need to use never more the procedural functions – BrachiopodDateTime
class internally uses the same code asstrtotime()
to parse the string provided as first argument to its constructor. The problem and the outcome is the same. – RidentThis section describes all the different formats that the strtotime(), DateTime and date_create() parser understands.
doesn't means that's the same code. However, it's better to use classes instead of functions if you start to coding in PHP 7. PHP people wants to improve the language image, to avoid java developers said php is a crap (due this kind of reasons) – BrachiopodDateTime
that supports the same formats asstrtotime()
supports, don't you think? If you need a solid proof then feel free to check the source code of PHP. Search for functiontimelib_strtotime()
(it implements the parsing) in fileext/date/php_date.c
. You will find it called bystrtotime()
, indirectly byDateTime::__construct()
, bydate_parse()
, byDateInterval::createFromDateString()
and some others. – Rident