php - 强制 php strtotime 使用 UTC

标签 php timestamp strtotime

我已经看到了一些关于这个的问题,但没有一个明确的答案...... strtotime() 在将字符串转换为 unix 时间戳时将使用为 PHP 设置的默认时区。

但是,我想将字符串转换为 UTC 中的 unix 时间戳。既然没有这样做的参数,那怎么办呢?

我要转换的字符串是:2011-10-27T20:23:39,它已经是 UTC 格式了。我想将其表示为 UTC 中的 unix 时间戳。

谢谢

最佳答案

我意识到这个问题很老了,但我找到了另一个可能有用的选项。 您可以像这样将时区指定为传递给 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

注意:我使用的是 PHP 5.5.9,但这应该适用于任何 php 版本。

关于php - 强制 php strtotime 使用 UTC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7924663/

相关文章:

mysql - 比较时间戳与 current_timestamp 在 mysql 中不起作用

php - C 中的 strtotime 函数

python - 使用 SQL 和 python 将时间单位添加到时间戳

php - 同时运行多个 cron 作业时如何克服服务器负载问题?

php - 从复选框中检索值

php - mysql_escape_string 漏洞

sql - Oracle,SQL,如何获取日期之间的间隔

php - 在 PHP 5.1 中添加月份的准确方法?

php - 如何直接在 strtotime 中获取四舍五入的分钟数

php - 检查 PHP 数组的连续索引