Bri*_*ian 10 php timestamp strtotime
我已经看到了一些关于这个的问题,但没有一个明确的答案......当将字符串转换为unix时间戳时,strtotime()将使用为PHP设置的默认时区.
但是,我想将字符串转换为UTC中的unix时间戳.由于没有这样做的参数,怎么办呢?
我想要转换的字符串是:2011-10-27T20:23:39,已经是UTC了.我想将其表示为UTC中的unix时间戳.
谢谢
Eri*_*and 15
我意识到这个问题已经很老了,但我找到了另一个可能有用的选项.您可以将时区指定为传递给strtotime的字符串的一部分,而不是设置然后还原php的时区,如下所示:
echo date_default_timezone_get();
output: America/Chicago
echo gmdate('Y-m-d H:i:s', strtotime('2011-10-27T20:23:39'));
output: 2011-10-28 01:23:39
echo gmdate('Y-m-d H:i:s', strtotime('2011-10-27T20:23:39 America/Chicago'));
output: 2011-10-28 01:23:39
echo gmdate('Y-m-d H:i:s', strtotime('2011-10-27T20:23:39 UTC'));
output 2011-10-27 20:23:39
Run Code Online (Sandbox Code Playgroud)
注意:我在PHP 5.5.9上,但这应该适用于任何PHP版本.
在strtotime
拨打电话前设置系统默认时区:
date_default_timezone_set('UTC');
echo strtotime('2011-10-27T20:23:39')."\n";
// Proof:
print_r(getdate(strtotime('2011-10-27T20:23:39')));
// IMPORTANT: It's possible that you will want to
// restore the previous system timezone value here.
// It would need to have been saved with
// date_default_timezone_get().
Run Code Online (Sandbox Code Playgroud)