用 PHP 在时区之间转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5503135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:38:33  来源:igfitidea点击:

Converting between timezones in PHP

phpdatetimetimezone

提问by FAFAFOHI

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

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.

我使用以下 PHP 代码来完成此操作,但我想将所有时区转换为 PST/PDT。我查看了 PHP 手册并看到了这一点,date_default_timezone_set()但我不确定如何将其实现到我下面的代码中。

$date = $messages[0]->CreationTime;
echo date('l F j Y g:i:s A I', strtotime($date))

回答by Matthew

I would not use date_default_timezone_setfor general TZ conversions. (To clarify... if this is for display purposes, script wide, then using the default timezone is a reasonable thing to do.)

我不会date_default_timezone_set用于一般的 TZ 转换。(澄清......如果这是为了显示目的,脚本范围,那么使用默认时区是合理的做法。)

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";

回答by Mike B

$date = $messages[0]->CreationTime;
date_default_timezone_set('America/Los_Angeles');
echo date('l F j Y g:i:s A I', strtotime($date));

See this listfor available timezones that get passed into the function

请参阅此列表以了解传递给函数的可用时区