$timezone = new DateTimeZone('Pacific/Apia');
$date = new DateTime('@1306123200', $timezone);
echo $date->format("Y-m-d H:i:s");
echo '<br />';
$date->setTimezone($timezone);
echo $date->format("Y-m-d H:i:s");为什么timezone改变其他时区就会改变日期时间?时间戳没有改变的喔
$date = new DateTime('@1306123200', $timezone);
echo $date->format("Y-m-d H:i:s");
echo '<br />';
$date->setTimezone($timezone);
echo $date->format("Y-m-d H:i:s");为什么timezone改变其他时区就会改变日期时间?时间戳没有改变的喔
(
[warning_count] => 1
[warnings] => Array
(
[6] => Double timezone specification
) [error_count] => 1
[errors] => Array
(
[0] => The timezone could not be found in the database
))因为:
The $timezone parameter and the current timezone are ignored when the $time parameter either is a UNIX timestamp (e.g. @946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00).你指定了一个UNIX时间戳, 所以所有时区包括参数时区与系统时区都被忽略, 直接把该UNIX时间戳当做UTC了. 然后又setTimezone了,然后format就会使用这个timezone,结果就是在原先UTC上偏移一下再打印,自然与你想想的有出入。
那么如果再setTimestamp多一次呢?会再偏移一下吗
打错是再setTimezone多一次
不是设置几次的意思... 设置一次就行了, 因为new那里设置的因为你用了@unix_timestamp而失效了,所以new那里完全没有时区的偏移,直接当做UTC了,然后你setzone了,然后format就是将那个UTC时间偏移到你设置的时区打印出来一个日期。UTC是计算机维持时间的一种方式,是1970年来的秒数,设置了timezone后,比如东8区,那么在UTC时间上偏移一些时间就是当前的北京时间了。 你在脑子里理顺理顺吧 -,- 个人感觉很多人当了很久程序员可能都没搞懂的东西就是时间和编码这两个玩意了,一点也不假。