PHP Timezone List

Ever need a list of US timezones, this class creates it.


/**
 * Timezone calculations
 */
class Timezone
{

    /**
     * Timezones list with GMT offset
     *
     * @return array
     * @link http://stackoverflow.com/a/9328760
     * @link http://php.net/manual/en/datetimezone.listidentifiers.php
     */
    public static function tzList()
    {
        $zones_array = array();
        $timestamp = time();
        foreach (timezone_identifiers_list(\DateTimeZone::PER_COUNTRY, 'US') as $key => $zone) {
            date_default_timezone_set($zone);
            $zones_array[$key]['zone']          = $zone;
            $zones_array[$key]['zone_friendly'] = self::formatTimezoneName($zone);
            $zones_array[$key]['diff_from_GMT'] = 'UTC/GMT ' . date('P', $timestamp);
        }
        return $zones_array;
    }

    /**
     * Format the zone name into a friendly format
     *
     * @method formatTimezoneName
     *
     * @param  string             $name The zone nmae
     *
     * @return string                   The formatted zone name
     */
    private static function formatTimezoneName($name)
    {
        $name = str_replace('/', ', ', $name);
        $name = str_replace('_', ' ', $name);
        $name = str_replace('St ', 'St. ', $name);
        return $name;
    }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s