Reputation: 3
I have timestamp from specific timezone (Jamaica) and i want to get GMT timestamp of it. Is there more elegant solution than this one :
$start = DateTime::createFromFormat('U', 1330560000);
$start->setTimezone(new DateTimeZone('America/Jamaica'));
$start->format('Y-m-d H:i:s');//2012-02-29 19:00:00 NO NO NO
$tz = new DateTimeZone( 'America/Jamaica' );
$transition = $tz->getTransitions($start->getTimestamp(),$start->getTimestamp());
$offset = $transition[0]['offset'];
$start = DateTime::createFromFormat('U', $params['start'] - 2*$transition[0]['offset']);
$start->setTimezone(new DateTimeZone('America/Jamaica'));
$start->format('Y-m-d H:i:s'); // "2012-03-01 05:00:00" YESSSS!!!
Upvotes: 0
Views: 943
Reputation: 32158
Unix time, or POSIX time, is a system for describing instances in time, defined as the number of seconds that have elapsed since midnight Coordinated Universal Time (UTC), 1 January 1970.
source Wikipedia
The idea of the UNIX timestamp is that it is always in UTC (everywhere in the world xD ). If it does not represents the time in UTC it's not a UNIX timestamp anymore
Upvotes: 1
Reputation: 1717
This is a part of my class that create a well-formatted time stamp as I wrote in the comment of the function, it's very easy to use just pass the string of the date, time zone and the identifier.
Hope it helps you
/**
* Default timestamp format for formatted_time
* @var string
*/
public static $timestamp_format = 'Y-m-d H:i:s';
/**
* Returns a date/time string with the specified timestamp format
* @example $time = Date::formatted_time('5 minutes ago');
* @link http://www.php.net/manual/datetime.construct
* @param string $datetime_str datetime string
* @param string $timestamp_format timestamp format
* @param string $timezone timezone identifier
* @return string
*/
public static function formatted_time($datetime_str = 'now', $timestamp_format = NULL, $timezone = NULL){
$timestamp_format = ($timestamp_format == NULL) ? Date::$timestamp_format : $timestamp_format;
$timezone = ($timezone === NULL) ? Date::$timezone : $timezone;
$tz = new DateTimeZone($timezone ? $timezone : date_default_timezone_get());
$time = new DateTime($datetime_str, $tz);
if ($time->getTimeZone()->getName() !== $tz->getName()){
$time->setTimeZone($tz);
}
return $time->format($timestamp_format);
}
Upvotes: 0