note 57022 deleted from function.putenv by danbrown
| From: | danbrown@php.net | Date: | Sun, 20 Jul 2008 01:02:24 +0000 |
| Subject: | note 57022 deleted from function.putenv by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-141880@lists.php.net to get a copy of this message | ||
Note Submitter: EricZ <ezsomethign at notthere dot com>
----
Okay, so here's the format that TZ is looking for
"America/New_York". You can get all of the timezones
from a single file on most unix systems. This code works
on all Linux systems that I have seen (various redhat/deb)
where you can find this file... And now you have a nice,
one level nested timezone list by country.
<?php
// -- 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);
?>