note 56392 added to ref.datetime
| From: | (ezsomethingathotmail) | Date: | Thu, 01 Sep 2005 09:56:13 +0000 |
| Subject: | note 56392 added to ref.datetime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-94570@lists.php.net to get a copy of this message | ||
I was having a horrible time trying to get a good list of timezones, how to set them locally (for
the user/client), and how best to keep this information. Building on the notes of this site (thanks
everyone), I constructed code that should work on just about any un*x based platform. It reads the
local timezone table and gives your a nested array of the continents and regions of the file -- the
natural continent sorting is even by the most populated areas (thanks Paul Eggert!).
Afterwards, all you have to do is export the timezone string with an environmental set.. which works
just fine if php is running as an apache model; haven't tested it for commandline, but I
suspect it's okay there, too.
<?php
if (isset($_SESSION[PROFILE_TZOFFSET]))
putenv('TZ='.$_SESSION[PROFILE_TZOFFSET]);
?>
Here's how to load the timezones... the nested array makes it easy to insert into html lists or
other well-behaved objects.
<?php
function getTimezoneData() {
//EricZ - 9/1/05 - free to use, keep the comments :)
$zoneNames = array();
// -- first part, gather all of the zone data
$zoneSource = '/usr/share/zoneinfo/zone.tab';
$zoneHandle = fopen($zoneSource, "r");
if (!$zoneHandle) return NULL; //bad file, abort now
while (!feof($zoneHandle)) {
$zoneLine = ltrim(fgets($zoneHandle, 4096));
if ($zoneLine[0]=='#') continue; //skip comments
//Columns...
// 1. ISO 3166 2-character country code.
// 2. Latitude and longitude of the zone's principal location
// 3. Zone name used in value of TZ environment variable.
// 4. Comments; present if and only if country has multiple rows.
$zoneParts = explode("\t",$zoneLine); //grab parts
if (count($zoneParts) < 3) continue; //erroneous line!
$nameParts = explode('/', $zoneParts[2], 2); //grab country/name
$zoneKey = $nameParts[0]; //country or area
$insertArray = &$zoneNames; //where to insert?
if (count($nameParts) > 1) { //area/populous
if (!isset($zoneNames[$zoneKey])) //not set before
$zoneNames[$zoneKey] = array();
$insertArray = &$zoneNames[$zoneKey]; //grab sub (country)
$zoneKey = trim($nameParts[1]); //grab correct key
}
$zoneKey = preg_replace('/[_]/',' ', $zoneKey);
$insertArray[$zoneKey] = trim($zoneParts[2]); //actually set data
} //end while not eof
fclose($zoneHandle);
return $zoneNames;
}
?>
----
Manual Page -- http://www.php.net/manual/en/ref.datetime.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+56392
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56392&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56392&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56392&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56392&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56392&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56392&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+56392&report=yes
Search -- http://master.php.net/manage/user-notes.php