Using PHP's listidentifiers (part of the DateTimeZone class ), we can create a dropdown menu of all available global timezone identifiers. It's handy for inclusion in forms if you're after a general region rather than just country.
The first function retrieves timezone values and returns an array:
1
<?php
2
/*
3
Get Global Timezones/Locations in Dropdown Select Menu
4
http://www.beliefmedia.com/code/php-snippets/timezone-select-menu
5
*/
6
7
function beliefmedia_get_timezones() {
8
$o = array();
9
10
11
foreach($t_zones as $a) {
12
$t = '';
13
14
try {
15
/* Throws exception for 'US/Pacific-New' */
16
$zone = new DateTimeZone($a);
17
18
$seconds = $zone->getOffset( new DateTime('now', $zone) );
19
20
21
22
$t = $a . ' [ ' . $hours . ':' . $minutes . ' ]';
23
$o[$a] = $t;
24
}
25
26
/* Exceptions must be catched, else a blank page */
27
catch(Exception $e) {
28
/* die("Exception : " . $e->getMessage() . ''); */
29
}
30
31
}
32
33
/* Sort array by key */
34
ksort($o);
35
36
return $o;
37
}
The second function retrieves the timezone array and manufactures the select menu.
1
<?php
2
/*
3
Get Global Timezones/Locations in Dropdown Select Menu
4
http://www.beliefmedia.com/code/php-snippets/timezone-select-menu
5
*/
6
7
8
9
/* Get timezones */
10
$o = beliefmedia_get_timezones();
11
12
foreach($o as $tz => $label) {
13
$return .= '<option value="' . $tz . '">' . $label . '</option>';
14
}
15
16
return '<select name="timezone" style="height: 30px; font-size: 1.0em;">' . $return . '</select>';
17
}
Usage is as follows:
/* Usage */ echo beliefmedia_timezones();
The results is as follows:
Note: I've included raw HTML to show the list so timezone values may not be correct.