将时间戳转换为时区

将时间戳转换为时区

问题描述:

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

I have a timestamp the user enters in GMT.

然后我想以gmt,cet,pst和est显示该时间戳.

I would then like to display that timestamp in 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;
}

您可以使用PHp 5的 .它允许对时区设置和输出进行非常细粒度的控制.与手册混在一起:

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

$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