php 将时间戳转换为时区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4186868/
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 12:14:00  来源:igfitidea点击:

Convert Timestamp to Timezones

phptimezone

提问by azz0r

I have a timestamp the user enters in GMT.

我有一个用户在 GMT 中输入的时间戳。

I would then like to display that timestamp in gmt, cet, pst, est.

然后我想在 gmt、cet、pst、est 中显示该时间戳。

Thanks to the post below I have made, which works perfectly!

感谢我在下面发表的帖子,它完美无缺!

public static function make_timezone_list($timestamp, $output='Y-m-d H:i:s P') {

    $return     = array();
    $date       = new DateTime(date("Y-m-d H:i:s", $timestamp));
    $timezones  = array(
        'GMT' => 'GMT', 
        'CET' => 'CET', 
        'EST' => 'EST', 
        'PST' => 'PST'
    );

    foreach ($timezones as $timezone => $code) {
        $date->setTimezone(new DateTimeZone($code));
        $return[$timezone] = $date->format($output);
    }
    return $return;
}

回答by Pekka

You could use PHp 5's DateTimeclass. It allows very fine-grained control over Timezone settings and output. Remixed from the manual:

您可以使用 PHp 5's DateTimeclass。它允许对时区设置和输出进行非常细粒度的控制。从手册中重新混合:

$timestamp = .......;


$date = new DateTime("@".$timestamp);  // will snap to UTC because of the 
                                       // "@timezone" syntax

echo $date->format('Y-m-d H:i:sP') . "<br>";  // UTC time

$date->setTimezone(new DateTimeZone('Pacific/Chatham'));   
echo $date->format('Y-m-d H:i:sP') . "<br>";  // Pacific time

$date->setTimezone(new DateTimeZone('Europe/Berlin'));
echo $date->format('Y-m-d H:i:sP') . "<br>";  // Berlin time