MeuMySQL Blog is now at http://blog.mdnsolutions.com/.

Tuesday, July 17, 2012

[OFF-TOPIC] Creating a List of Timezones with PHP

Hey guys,

Below you can find how to create a list of timezones with PHP using the native object DateTimeZone. Enjoy!


<?php

class TimeZone
{

    static $regions = array(
        'Africa' => \DateTimeZone::AFRICA,
        'America' => \DateTimeZone::AMERICA,
        'Antarctica' => \DateTimeZone::ANTARCTICA,
        'Asia' => \DateTimeZone::ASIA,
        'Atlantic' => \DateTimeZone::ATLANTIC,
        'Europe' => \DateTimeZone::EUROPE,
        'Indian' => \DateTimeZone::INDIAN,
        'Pacific' => \DateTimeZone::PACIFIC,
    );

    /**
     * 
     * @return array List of locations
     */
    static public function getLocations()
    {
        foreach (self::$regions as $name => $mask) {
            $tzlist = \DateTimeZone::listIdentifiers($mask);
            foreach ($tzlist as $tz) {
                $locations[$name][$tz] = substr($tz, strrpos($tz, '/') + 1);
            }
        }
        
        return $locations;
    }

}

?/>

To use it just write in your code:

<?php
$locations = TimeZone::getLocations();
?>

No comments:

Post a Comment