SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Thread: international time zones array

  1. #1
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    international time zones array

    i'm trying to use the DateTime listAbbreviations method (http://au.php.net/manual/en/datetime...reviations.php) to get an array of international timezones.

    The problem is, the array is absolutely huge. Apparently there is alot of historical timezones stored, which i'm not really interested in. My question is, how can I go about getting a short list that represents all the time zones without having repetitive zones - is anyone aware of the important keys contained in the array? Surely i'm not forced to extract useful info out of this monster array?
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  2. #2
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    914
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What do you mean by 'important keys'?

    If there are many timezones with the same offset, how do you decide which one is more important that the other?

  3. #3
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well for example there is Currie and Linderman listed under Australia, both places i've never heard of that share the same time zone as Sydney which is the obvious choice for anyone in New South Wales. Additionally there is two listings for Sydney, 1 contains DST = true while the other is DST = false. DST should be true for Sydney, I have no idea why there is the two results.
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  4. #4
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    914
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wheeler View Post
    well for example there is Currie and Linderman listed under Australia, both places i've never heard of that share the same time zone as Sydney which is the obvious choice for anyone in New South Wales. Additionally there is two listings for Sydney, 1 contains DST = true while the other is DST = false. DST should be true for Sydney, I have no idea why there is the two results.
    I see. I write a small class that extends timezone class in order to generate a nice looking drop-down list where user can select his own timezone.

    I just added an option so you can define your own 'Important' zones.
    You just have to fill the array 'addImportantZones' by hand and include only zones you want in it.
    Then when you call $oTz = new clsTz();
    $arrTz = $oTz->getSelectArray();

    try it, see if it works for you.

    Code:
    class clsTZ extends DateTimeZone
    {
    
    	private $arrImportantZones = array(); // add names of important zones here
    	
    	public function getSelectArray(){
    		$arrResult = array();
    		$arr = self::listAbbreviations();
    
    		foreach($arr as $abbr){
    			foreach($abbr as $aTz){
    				$key = $aTz['timezone_id'];
    				if(!in_array($key, $this->arrImportantZones)){
    					continue;
    				}
    				$sign   = ($aTz['offset'] < 0) ? '-' : ( ($aTz['offset'] > 0) ? '+' : '') ;
    				$gmt   = abs($aTz['offset']) / 3600 ;
    				$hh     = floor($gmt) ;
    				$mm     = $gmt - $hh ;
    				$mm  = floor($mm*60) ;
    
    				$val = '(GMT'.$sign.sprintf("%02s",$hh).':'.sprintf("%02s",$mm).') '.$aTz['timezone_id'];
    				if(!empty($key) && !empty($val)){
    					$arrResult[$key] = $val;
    				}
    			}
    		}
    
    		return $arrResult;
    	}
    }

  5. #5
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I couldn't get this to work - for starters I can't extend DateTimeZone (fatal error), and it doesn't seem to be returning any results even though I modified the listAbbreviations line.

    I'm thinking i'm just going to create a custom list from windows clock -> timezone or adapt it from something like phpBB registration. Just checked, 5049 time zones in DateTimeZone::listAbbreviations()!
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  6. #6
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    914
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wheeler View Post
    I couldn't get this to work - for starters I can't extend DateTimeZone (fatal error), and it doesn't seem to be returning any results even though I modified the listAbbreviations line.

    I'm thinking i'm just going to create a custom list from windows clock -> timezone or adapt it from something like phpBB registration. Just checked, 5049 time zones in DateTimeZone::listAbbreviations()!
    That's strange because it worked for me. What php version are you using?

  7. #7
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    5.2
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  8. #8
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    914
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, I see the problem.
    I my own version of this class I don't actually instantiate the extended class and instead using it as a static method



    You can also do this, then pass your own array $arrImportantZones to this function

    Then the class will look like this:

    Code:
    class clsTZ extends DateTimeZone
    {
    	
    	public function getSelectArray($arrImportantZones){
    		$arrResult = array();
    		$arr = self::listAbbreviations();
    
    		foreach($arr as $abbr){
    			foreach($abbr as $aTz){
    				$key = $aTz['timezone_id'];
    				if(!in_array($key, $arrImportantZones)){
    					continue;
    				}
    				$sign   = ($aTz['offset'] < 0) ? '-' : ( ($aTz['offset'] > 0) ? '+' : '') ;
    				$gmt   = abs($aTz['offset']) / 3600 ;
    				$hh     = floor($gmt) ;
    				$mm     = $gmt - $hh ;
    				$mm  = floor($mm*60) ;
    
    				$val = '(GMT'.$sign.sprintf("%02s",$hh).':'.sprintf("%02s",$mm).') '.$aTz['timezone_id'];
    				if(!empty($key) && !empty($val)){
    					$arrResult[$key] = $val;
    				}
    			}
    		}
    
    		return $arrResult;
    	}
    }
    
    $arrTz = clsTZ::getSelectArray($arrImportantZones);

  9. #9
    SitePoint Evangelist simshaun's Avatar
    Join Date
    Apr 2008
    Location
    North Carolina
    Posts
    438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Download CodeIgniter and open up /system/helpers/date_helper.php

    Find function timezones($tz = '') and you'll see an array there.
    You could use that array (or the entire date_helper function-set with a little modification.)
    A description of that array is at the bottom of the page here.

  10. #10
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks, but unfortunately this list is incomplete - I know for sure its missing Australia/Brisbane, which is different from Sydney, Melbourne in that there is no DST. I could obviously add it in but I wonder which other regions might be missing.

    Who would've thought this would be so hard! The prospect of hand coding a list is equally unappealing seeing as I need to search the php array for the appropriate time zone name for every zone.

    Here is the only method I know of to convert a time:
    PHP Code:
    // Create two timezone objects, one for server and one for user
    $utc = new DateTimeZone('UTC');
    $user = new DateTimeZone($user_timezone); // looks like Australia/Brisbane or US/Pacific
                
    // Create two DateTime objects that will contain the same Unix timestamp, but have different timezones attached to them.
    $date_utc = new DateTime('now'$utc);
    $date_user = new DateTime('now'$user);
                
    $time_offset $user->getOffset($date_utc); 
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  11. #11
    SitePoint Evangelist simshaun's Avatar
    Join Date
    Apr 2008
    Location
    North Carolina
    Posts
    438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  12. #12
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,633
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Who would've thought this would be so hard!
    Me!
    The fact that DST can start and end at different times each year for the same location also makes things complex.
    mikehealy.com.au
    diigital.com art, design . Latest WorkSaturday Morning

  13. #13
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm a bit confused about this page:

    http://au.php.net/manual/en/class.datetimezone.php

    Is there a way I can return the ALL array which supposedly only contains current time zones (not the backwards compatible ones)?
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •