note 99688 deleted from datetimezone.listabbreviations by googleguy

From: Date: Tue, 22 Oct 2013 06:45:38 +0000
Subject: note 99688 deleted from datetimezone.listabbreviations by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-196623@lists.php.net to get a copy of this message
Note Submitter: the_dark_lord12001 at yahoo dot com ---- With the new dateTime functionality of PHP 5, i found that the dateTime::listabreviations and the dateTime::listIdentifiers return invalid time zone continent/city pairs that throws an error saying time zone not found in data base. So i wrote a class function that will return the abbreviated valid time zones I hope this helps someone, and i would appreciate some tips on how to improve this class. <?php class TimeZones { public $zones = array(); public $countries = array(); /** * Empty construct */ public function __construct(){} /** * Utility function to re-index the array returned * from getTimeZones(). The $a is integer indexed * from the order of DateTimeZone::listIdentifiers() * so we need to reindex it to match proper formating * * @param array; * @return new indexed array; */ protected function cmp ( $array ) { $i=0; if ( !is_array ( $array ) ) return; foreach ( $array as $key => $val ) { if ( is_array ( $val ) ) { foreach ( $val as $k => $v ) { if ( is_array ( $v ) ) { foreach ( $v as $a => $b ) { if ( is_array ( $b ) ) { foreach ( $b as $z => $y ) { if ( $newK != $k ) $i=0; $arr[$key][$k][$i][$z] = $y; $newK = $k; } $i++; } } } } } } return $arr; } /** * This function takes an array of the old style of time zone * abreviations. i.e. CST, EST, PST * * @param array; * @return none; sets $this->zones variable */ public function setTimeZones ( $array ) { if ( !is_array($array) ) return; $this->zones = $array; } /** * This function takes an array of the continent names for which * you want to get the valid time zone names for * * @param array; * @return none; sets $this->countries variable */ public function setCountries ( $array ) { if ( !is_array($array) ) return; $this->countries = $array; } /** * This function is similiar to the dateTime::listabbreviations method * but this one weeds out those time zones that are not recognized. * * @param none; * @return array; a multidimensional [country][time zone abreviation][city/offset] */ public function getTimeZones() { $ident = DateTimeZone::listIdentifiers(); $cntIdent = count($ident); $zones = $this->zones; $cntZones = count($zones); for ( $i=0; $i<$cntZones; $i++ ) { for ( $j=0; $j<$cntIdent; $j++ ) { if ( date_default_timezone_set($ident[$j]) ) { try { if ( !$date = new dateTime($ident[$j]) ) { throw new Exception ($e); } else { $zoneFormat = $date->format('T'); $tz = $date->getTimezone(); $tzName = $tz->getName(); $offset = $date->getOffset(); $utc[$j] = array ( 'city' => $tzName, 'offset' => $offset ); if ( strtoupper($zones[$i]) == $zoneFormat ) { $ex = explode ( '/', $tzName ); sort($this->countries); for ( $z=0; $z<count($this->countries); $z++ ) { if ( $ex[0] == ucwords($this->countries[$z]) ) { $array[$ex[0]][$zoneFormat][$j] = array ( 'city' => $tzName, 'offset' => $offset ); } } } } } catch(Exception $e) {} } } } $arr = $this->cmp($array); return $arr; } } $class = new TimeZones(); $array = array ( 'cdt', 'edt', 'mst', 'pdt' ); $country = array ('America'); $class->setCountries($country); $class->setTimeZones($array); print_r($class->getTimeZones()); ?>

« previous php.notes (#196623) next »