Categories
PHP

PHP Timezones selector

Long time a go I wrote about how to get timezones selector.

Well here there’s an improve way to get this select, with few options.

Result:

Usage:

echo get_select_timezones('TIMEZONE',date_default_timezone_get());


The PHP code:

///timezones functions
function get_timezones()
{
    if (method_exists('DateTimeZone','listIdentifiers'))
    {
        $timezones = array();
        $timezone_identifiers = DateTimeZone::listIdentifiers();

        foreach( $timezone_identifiers as $value )
        {
            if ( preg_match( '/^(America|Antartica|Arctic|Asia|Atlantic|Europe|Indian|Pacific)//', $value ) )
            {
                $ex=explode('/',$value);//obtain continent,city
                $city = isset($ex[2])? $ex[1].' - '.$ex[2]:$ex[1];//in case a timezone has more than one
                $timezones[$ex[0]][$value] = $city;
            }
        }
        return $timezones;
    }
    else//old php version
    {
        return FALSE;
    }
}



function get_select_timezones($select_name='TIMEZONE',$selected=NULL)
{
    $timezones = get_timezones();
    $sel.='';

    return $sel;
}