note 89155 added to function.timezone-name-from-abbr

From: Date: Tue, 24 Feb 2009 15:03:51 +0000
Subject: note 89155 added to function.timezone-name-from-abbr
Groups: php.notes 
Request: Send a blank email to php-notes+get-151068@lists.php.net to get a copy of this message
Another way to do this is to wrap the function in a class that extends the DateTimeZone class: <?php /** * Helps with timezones. * @link http://us.php.net/manual/en/class.datetimezone.php * * @package Date */ class Helper_DateTimeZone extends DateTimeZone { /** * Converts a timezone hourly offset to its timezone's name. * @example $offset = -5, $isDst = 0 <=> return value = 'America/New_York' * * @param float $offset The timezone's offset in hours. * Lowest value: -12 (Pacific/Kwajalein) * Highest value: 14 (Pacific/Kiritimati) * @param bool $isDst Is the offset for the timezone when it's in daylight * savings time? * * @return string The name of the timezone: 'Asia/Tokyo', 'Europe/Paris', ... */ final public static function tzOffsetToName($offset, $isDst = null) { if ($isDst === null) { $isDst = date('I'); } $offset *= 3600; $zone = timezone_name_from_abbr('', $offset, $isDst); if ($zone === false) { foreach (timezone_abbreviations_list() as $abbr) { foreach ($abbr as $city) { if ((bool)$city['dst'] === (bool)$isDst && strlen($city['timezone_id']) > 0 && $city['offset'] == $offset) { $zone = $city['timezone_id']; break; } } if ($zone !== false) { break; } } } return $zone; } } ?> Then you could do something like this: <?php $Dtz = new Helper_DateTimeZone(Helper_DateTimeZone::tzOffsetToName(-5)); var_dump($Dtz->getName()); string(16) "America/New_York" ?> ---- Server IP: 209.41.74.194 Probable Submitter: 66.138.198.129 ---- Manual Page -- http://www.php.net/manual/en/function.timezone-name-from-abbr.php Edit -- https://master.php.net/note/edit/89155 Del: integrated -- https://master.php.net/note/delete/89155/integrated Del: useless -- https://master.php.net/note/delete/89155/useless Del: bad code -- https://master.php.net/note/delete/89155/bad+code Del: spam -- https://master.php.net/note/delete/89155/spam Del: non-english -- https://master.php.net/note/delete/89155/non-english Del: in docs -- https://master.php.net/note/delete/89155/in+docs Del: other reasons-- https://master.php.net/note/delete/89155 Reject -- https://master.php.net/note/reject/89155 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#151068) next »