在 PHP 中将时区设置为 UTC (0)
为什么会这样?
date_default_timezone_set('Australia/Currie');
但这似乎根本没有任何效果?
But this doesn't seem to take any effect at all?
date_default_timezone_set('UTC');
将时区设置为 UTC 时,此值不会改变:
This value doesn't change when setting the timezone to UTC:
echo date('Y-m-d H:i:s', time());
我使用的是php 5.2.13,我的服务器时区是:
I'm using php 5.2.13, and the timezone of my server is:
$server_tz = date_default_timezone_get();
echo $server_tz; //outputs 'America/Guayaquil'
这是原始代码:
echo time() . "<br>
";
date_default_timezone_set('UTC');
echo time() . "<br>
";
输出:
1317235130
1317235130
推荐答案
问题是您正在显示 time()
,它是基于 GMT/UTC 的 UNIX 时间戳.这就是它不改变的原因.另一方面,date()
格式化基于该时间戳的时间.
The problem is that you're displaying time()
, which is a UNIX timestamp based on GMT/UTC. That’s why it doesn’t change. date()
on the other hand, formats the time based on that timestamp.
timestamp 是自 Unix 纪元(格林威治标准时间 1970 年 1 月 1 日 00:00:00)以来的秒数.
A timestamp is the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
echo date('Y-m-d H:i:s T', time()) . "<br>
";
date_default_timezone_set('UTC');
echo date('Y-m-d H:i:s T', time()) . "<br>
";
相关文章