将时间戳转换为时区 [英] Convert Timestamp to Timezones

查看:1234
本文介绍了将时间戳转换为时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户在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    

这篇关于将时间戳转换为时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆