RSS

Tag Archives: timezone

How to get TimeZone Offset in PHP

timezone

timezone

function getOffsetByTimeZone($localTimeZone)
{
$time = new DateTime(date('Y-m-d H:i:s'), new DateTimeZone($localTimeZone));
$timezoneOffset = $time->format('P');
return $timezoneOffset;
}

Call this function passing a local time zone as a string.
eg.

getOffsetByTimeZone('America/New_York');
 
Leave a comment

Posted by on March 20, 2013 in PHP

 

Tags: ,

How to switch between Timezones in PHP

timezone

timezone

[Extracted from : http://www.mindfiresolutions.com/PHP-function-for-Time-Zone-conversion-56.php]

Convert from GMT to a local timezone

function ConvertGMTToLocalTimezone($gmttime,$timezoneRequired)
{
$system_timezone = date_default_timezone_get();

date_default_timezone_set("GMT");
$gmt = date("Y-m-d h:i:s A");

$local_timezone = $timezoneRequired;
date_default_timezone_set($local_timezone);
$local = date("Y-m-d h:i:s A");

date_default_timezone_set($system_timezone);
$diff = (strtotime($local) - strtotime($gmt));

$date = new DateTime($gmttime);
$date->modify("+$diff seconds");
$timestamp = $date->format("Y-m-d h:i:s A");
return $timestamp;

}

Convert from local timezone to GMT

function ConvertLocalTimezoneToGMT($gmttime,$timezoneRequired)
{
$system_timezone = date_default_timezone_get();

$local_timezone = $timezoneRequired;
date_default_timezone_set($local_timezone);
$local = date("Y-m-d h:i:s A");

date_default_timezone_set("GMT");
$gmt = date("Y-m-d h:i:s A");

date_default_timezone_set($system_timezone);
$diff = (strtotime($gmt) - strtotime($local));

$date = new DateTime($gmttime);
$date->modify("+$diff seconds");
$timestamp = $date->format("Y-m-d h:i:s A");
return $timestamp;
}

Convert from one local timezone to another local timezone

function ConvertOneTimezoneToAnotherTimezone($time,$currentTimezone,$timezoneRequired)
{
$system_timezone = date_default_timezone_get();
$local_timezone = $currentTimezone;
date_default_timezone_set($local_timezone);
$local = date("Y-m-d h:i:s A");

date_default_timezone_set("GMT");
$gmt = date("Y-m-d h:i:s A");

$require_timezone = $timezoneRequired;
date_default_timezone_set($require_timezone);
$required = date("Y-m-d h:i:s A");

date_default_timezone_set($system_timezone);

$diff1 = (strtotime($gmt) - strtotime($local));
$diff2 = (strtotime($required) - strtotime($gmt));

$date = new DateTime($time);
$date->modify("+$diff1 seconds");
$date->modify("+$diff2 seconds");
$timestamp = $date->format("Y-m-d h:i:s A");
return $timestamp;
}
echo ConvertGMTToLocalTimezone("2013-03-16 14:28:00","Asia/Seoul")."
";
echo ConvertLocalTimezoneToGMT("2013-03-16 14:28:00","Asia/Seoul")."
";
echo ConvertOneTimezoneToAnotherTimezone("2013-03-16 14:28:00","Asia/Seoul","Australia/South");
 
1 Comment

Posted by on March 16, 2013 in PHP

 

Tags: