我有一个用户输入GMT的时间戳.
然后我想在gmt,cet,pst,est中显示该时间戳.
感谢我下面的帖子,它完美无缺!
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;
}
Run Code Online (Sandbox Code Playgroud)
Pek*_*ica 38
你可以使用PHp 5的DateTime课程.它允许对时区设置和输出进行非常精细的控制.从手册中重新混合:
$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
Run Code Online (Sandbox Code Playgroud)