在 PHP 中的时区之间转换

2022-01-16 00:00:00 datetime timezone php

我正在转换这个时间和日期:

I am converting this time and date:

Thu, 31 Mar 2011 02:05:59 GMT

转为以下时间日期格式:

To the following time and date format:

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')."
";

相关文章