在PHP之间转换时区 [英] Converting between timezones in PHP
问题描述
我正在转换这个时间和日期:
I am converting this time and date:
Thu, 31 Mar 2011 02:05:59 GMT
以下时间和日期格式:
Monday March 28 2011 4:48:02 PM
我正在使用遵循PHP代码来完成此操作,但我想将所有时区转换为PST / PDT。我查看了PHP手册,看到这个 date_default_timezone_set()
,但我不知道如何将它实现到下面的代码中。
I am using the following PHP code to accomplish this, but I want to convert all time zones to PST/PDT. I looked at the PHP manual and saw this date_default_timezone_set()
but I am not sure how to implement that into the code I have below.
$date = $messages[0]->CreationTime;
echo date('l F j Y g:i:s A I', strtotime($date))
推荐答案
我不会使用 date_default_timezone_set
进行一般的TZ转换。 (要澄清...如果这是为了显示目的,脚本广泛,那么使用默认时区是一件合理的事情。)
I would not use date_default_timezone_set
for general TZ conversions. (To clarify... if this is for display purposes, script wide, then using the default timezone is a reasonable thing to do.)
相反,我会使用某些东西如:
Instead, I would use something like:
$tz = new DateTimeZone('America/Los_Angeles');
$date = new DateTime('Thu, 31 Mar 2011 02:05:59 GMT');
$date->setTimezone($tz);
echo $date->format('l F j Y g:i:s A I')."\n";
这篇关于在PHP之间转换时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!