Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strtotime('0000-00-00 00:00') return negative value

Php strtotime('0000-00-00 00:00') showing strange behavior. Its returning negative value something like -62169984000.

I have Php version 5.4.17 in my 64-bit System. It should return false value which is expected.

But when I checked in other 32-bit system its returning false.

like image 302
pankaj Avatar asked Jul 23 '13 09:07

pankaj


People also ask

What does Strtotime return?

Return Values PHP strtotime() function returns a timestamp value for the given date string. Incase of failure, this function returns the boolean value false.

Why is Strtotime return false?

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.

How does Strtotime work in PHP?

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.

How do you add hours on Strtotime?

You can use DateTime::modify to add time, but I would just do time()+10800 . Show activity on this post. $time = new DateTime("+ 3 hour"); $timestamp = $time->format('Y-M-d h:i:s a');


1 Answers

On your system integers are 64 bits, so there is enough range to count seconds from the Unix epoch all the way back to 0 AD. Therefore strtotime works as advertised and returns a (very large) negative number. The return value is correct, your expectation is not.

In a 32-bit system the integer range is only sufficient to cover a ~68 year period, so going back earlier than about 1970 - 68 = 1902 will result in false being returned. Dates between 1902 and 1970 will still result in negative numbers.

like image 126
Jon Avatar answered Oct 27 '22 11:10

Jon