How do I get the country ISO from PHP timezone like America/Los_Angeles?

If I have "America/Los_Angeles" how do I get the two-digit country ISO this timezone belongs to?
Zoe ZoeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
I've never seen a class that can do this perfectly.  But there are a finite number of countries and timezones, so it seems possible to create the Venn diagram / translation table.  Here are some resources:
https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
http://php.net/manual/en/function.geoip-time-zone-by-country-and-region.php
http://php.net/manual/en/datetimezone.getlocation.php
http://stackoverflow.com/questions/2826419/country-to-timezones-in-php-zend-framework
Ray PaseurCommented:
The answer is in there, but it takes a little creativity to get it out.  
http://iconoun.com/demo/temp_shzoechen.php
<?php // demo/temp_shzoechen.php

/**
 * http://www.experts-exchange.com/questions/28700711/How-do-I-get-the-country-ISO-from-PHP-timezone-like-America-Los-Angeles.html
 *
 * REF:
 * http://php.net/manual/en/datetimezone.construct.php
 * http://php.net/manual/en/datetimezone.listidentifiers.php
 * http://php.net/manual/en/datetimezone.getlocation.php
 */
error_reporting(E_ALL);


// A CLASS TO HOLD TIMEZONE-TO-ISO_3166 DATA
Class CountryTimeZones
{
    public $zones = [];
    public function add($php_tz, $iso_3166)
    {
        $this->zones[$php_tz] = $iso_3166;
    }
    public function getCountry($php_tz)
    {
        return array_key_exists($php_tz, $this->zones)
        ? $this->zones[$php_tz]
        : FALSE
        ;
    }
}


// COLLECT THE DATA SET
$ctz = new CountryTimeZones;
$arr = DateTimeZone::listIdentifiers(DateTimeZone::ALL);

foreach ($arr as $tz_name)
{
    $obj = new DateTimeZone($tz_name);
    $sub = $obj->getLocation();
    $iso = $sub['country_code'];
    $ctz->add($tz_name, $iso);
}


// DEMONSTRATE THE CLASS
echo '<pre>';
var_dump($ctz->getCountry('America/New_York'));
var_dump($ctz->getCountry('America/Panama'));
var_dump($ctz->getCountry('America/Los_Angeles'));
var_dump($ctz->getCountry('UTC'));
var_dump($ctz->getCountry('wtf'));


// SAVE THE DATA SET
var_export($ctz);

Open in new window

Ray PaseurCommented:
Just a sidebar note... PHP has been inconsistent about capitalization of the timezone names in the past.  I do not know if this has been corrected in your version of PHP, but it's worth being aware of the risk.

America/New_York != America/New_york

Might be worth normalizing the data.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.