强制 php strtotime 使用 UTC [英] force php strtotime to use UTC
问题描述
我已经看到了一些关于此的问题,但没有明确的答案...当将字符串转换为 unix 时间戳时,strtotime() 将使用为 PHP 设置的默认时区.
I've seen a few questions about this but not a clear answer... strtotime() will use the default timezone set for PHP when converting the string to a unix timestamp.
但是,我想将字符串转换为 UTC 中的 unix 时间戳.既然没有参数,那怎么做呢?
However, I want to convert a string to unix timestamp in UTC. Since there is no parameter for doing so, how can this be done?
我尝试转换的字符串是:2011-10-27T20:23:39,它已经是 UTC.我想将其表示为 UTC 中的 Unix 时间戳.
The string I'm trying to convert is: 2011-10-27T20:23:39, which is already in UTC. I want to represent that as a unix timestamp also in UTC.
谢谢
推荐答案
在调用 strtotime
之前设置系统默认时区:
Set the system default timezone before making the strtotime
call:
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().
这篇关于强制 php strtotime 使用 UTC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!