开发者

Convert Timestamp to Timezones

I have a timestamp the user enters in GMT.

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'开发者_JAVA技巧
    );

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


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    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜