Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Timestamp to Timezones

Tags:

timezone

php

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;
}
like image 778
azz0r Avatar asked Nov 15 '10 17:11

azz0r


People also ask

Can I get timezone from timestamp?

You cannot “ get a TimeZone ID from a certain TimeStamp”, that is impossible. Your count-from-epoch was made while accounting for a certain time zone, usually UTC. If must know that intended zone used in creating that count-from-epoch, it cannot be deduced.

Is timestamp same for all timezones?

The UNIX timestamp is the number of seconds (or milliseconds) elapsed since an absolute point in time, midnight of Jan 1 1970 in UTC time. (UTC is Greenwich Mean Time without Daylight Savings time adjustments.) Regardless of your time zone, the UNIX timestamp represents a moment that is the same everywhere.

How do I convert epoch time to GMT?

=(A1 / 86400) + 25569 Format the result cell for date/time, the result will be in GMT time (A1 is the cell with the epoch number). For other time zones: =((A1 +/- time zone adjustment) / 86400) + 25569.

What timezone is timestamp?

For timestamp with time zone , the internally stored value is always in UTC (Universal Coordinated Time, traditionally known as Greenwich Mean Time, GMT ).


1 Answers

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    
like image 120
Pekka Avatar answered Oct 09 '22 22:10

Pekka