Wednesday, January 19, 2011

Get TimeZone with Daylight saving in PHP

function timeZones()
{
$list = DateTimeZone::listAbbreviations();
$idents = DateTimeZone::listIdentifiers();

$data = $offset = $added = array();
foreach ($list as $abbr => $info)
{
foreach ($info as $zone)
{
if ( ! empty($zone['timezone_id']) &&
! in_array($zone['timezone_id'], $added))
{
$z = new DateTimeZone($zone['timezone_id']);
$c = new DateTime('now', $z);
$value['offset'] = formatOffset($z->getOffset($c));

if($zone['dst'])
$day_light_saving = 'DST Yes';
else
$day_light_saving = 'DST No';

$value['timezone'] = $c->format('H:i a')." -- GMT ".$value['offset']." ".$zone['timezone_id']."--".$day_light_saving;
$data[$zone['timezone_id']] = $value;
$added[] = $zone['timezone_id'];
}
}

}

ksort($data);
return $data;

}

static private function formatOffset($offset)
{
$hours = $offset / 3600;
$remainder = $offset % 3600;
$sign = $hours > 0 ? '+' : '-';
$hour = (int) abs($hours);
$minutes = (int) abs($remainder / 60);

return $sign . str_pad($hour, 2, '0', STR_PAD_LEFT)
.':'. str_pad($minutes,2, '0');

}

Made simple correction from the below link discussions
http://stackoverflow.com/questions/1727077/generating-a-drop-down-list-of-timezones-with-php

No comments:

Post a Comment