开发者

Why is strtotime() working differently on different servers?

开发者 https://www.devze.com 2023-04-11 10:36 出处:网络
<?php header(\'Content-Type: text/plain\'); $time = \'0000-00-00 00:00:00\'; echo $time . PHP_EOL . PHP_EOL;
<?php

header('Content-Type: text/plain');

$time = '0000-00-00 00:00:00';
echo $time . PHP_EOL . PHP_EOL;

$time = strtotime($time);
echo $time . PHP_EOL . PHP_EOL;

On our development server, the second $time outputs nothing. A test with var_dump() reveals its value to be false. Which is fine. On a live server, I get 943920000. A test with var_dump() shows that th开发者_C百科is is an integer.

Why?

Apparently it is fairly common for dates of zero to come up as 30 November 1999. Why? And why on some servers and not on others?


try

setlocale(LC_ALL, "C");

before any strtotime call


Reading the PHP Manual page for strtotime(), we get the following:

Returns a timestamp on success, FALSE otherwise.

Therefore it follows that on one of your servers, the call to strtotime() is failing.

So why would it fail? The answer to that is also on the same manual page:

Errors/Exceptions

Every call to a date/time function will generate a E_NOTICE if the time zone is not valid, and/or a E_STRICT or E_WARNING message if using the system settings or the TZ environment variable. See also date_default_timezone_set()

So it seems to me that your one server doesn't have it's timezone locale configured correctly.

All that said and done, I would suggest not using strtotime() for stuff like this, if possible. It is a bit of a clunky old function, and the newer PHP datetime library provides much better/cleaner functionality. You can create a datetime object using it as follows:

$date = new DateTime('0000-00-00 00:00:00');

hope that helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消