I normaly use this script to allow the user choose his timezone:
<?php
///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.='<select id="'.$select_name.'" name="'.$select_name.'">';
foreach( $timezones as $continent=>$timezone )
{
$sel.= '<optgroup label="'.$continent.'">';
foreach( $timezone as $city=>$cityname )
{
if ($selected==$city)
{
$sel.= '<option selected=selected value="'.$city.'">'.$cityname.'</option>';
}
else $sel.= '<option value="'.$city.'">'.$cityname.'</option>';
}
$sel.= '</optgroup>';
}
$sel.='</select>';
return $sel;
}
?>
DateTimeZone::listIdentifiers
timezone_identifiers_list
(PHP 5 >= 5.2.0)
DateTimeZone::listIdentifiers -- timezone_identifiers_list — Returns numerically index array with all timezone identifiers
설명
객체 기반 형식
public static array DateTimeZone::listIdentifiers
([ int $what = DateTimeZone::ALL
[, string $country = NULL
]] )
절차식 형식
인수
- what
-
One of DateTimeZone class constants.
- country
-
A two-letter ISO 3166-1 compatible country code.
Note: This option is only used when what is set to DateTimeZone::PER_COUNTRY.
반환값
Returns array on success or FALSE on failure.
변경점
| 버전 | 설명 |
|---|---|
| 5.3.0 | Added the optional what and country parameters. |
예제
Example #1 A timezone_identifiers_list() example
<?php
$timezone_identifiers = DateTimeZone::listIdentifiers();
for ($i=0; $i < 5; $i++) {
echo "$timezone_identifiers[$i]\n";
}
?>
위 예제의 출력 예시:
Africa/Abidjan Africa/Accra Africa/Addis_Ababa Africa/Algiers Africa/Asmara
neo22s at gmail dot com
31-Oct-2011 05:50
chema at garridodiaz dot com
19-Feb-2010 02:50
I use this time zone select:
<select id="TIMEZONE" name="TIMEZONE">
<?php
$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
if ($continent!=$ex[0]){
if ($continent!="") echo '</optgroup>';
echo '<optgroup label="'.$ex[0].'">';
}
$city=$ex[1];
$continent=$ex[0];
echo '<option value="'.$value.'">'.$city.'</option>';
}
}
?>
</optgroup>
</select>
