14

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'
    );

    foreach ($timezones as $timezone => $code) {
        $date->setTimezone(new DateTimeZone($code));
        $return[$timezone] = $date->format($output);
    }
    return $return;
}
azz0r
  • 3,283
  • 7
  • 42
  • 85
  • Lookup the timezone offsets relative to GMT and add that to your current timestamp in GMT. – Gumbo Nov 15 '10 at 17:05
  • perfect answer for timezone... http://stackoverflow.com/a/3905222/1266559 – Alex Oct 25 '13 at 20:35
  • Came here through Google. The example in your question is the same as the accepted answer (it uses DateTIme), I don't understand what is the question since you say "It's working perfectly". Also I don't understand why you accepted an answer that is doing exactly the same thing as the code in your question. – mastazi Feb 11 '16 at 04:34

1 Answers1

44

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    
Pekka
  • 442,112
  • 142
  • 972
  • 1,088