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
8
function beliefmedia_get_timezones() {
9
$o = array();
10
11
12
foreach($t_zones as $a) {
13
$t = '';
14
15
try {
16
/* Throws exception for 'US/Pacific-New' */
17
$zone = new DateTimeZone($a);
18
19
$seconds = $zone->getOffset( new DateTime('now', $zone) );
20
21
22
23
$t = $a . ' [ ' . $hours . ':' . $minutes . ' ]';
24
$o[$a] = $t;
25
}
26
27
/* Exceptions must be catched, else a blank page */
28
catch(Exception $e) {
29
/* die("Exception : " . $e->getMessage() . '<br />'); */
30
}
31
32
}
33
34
/* Sort array by key */
35
36
37
return $o;
38
}
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
10
/* Get timezones */
11
$o = beliefmedia_get_timezones();
12
13
foreach($o as $tz => $label) {
14
$return .= '<option value="' . $tz . '">' . $label . '</option>';
15
}
16
17
return '<select name="timezone" style="height: 30px; font-size: 1.0em;">' . $return . '</select>';
18
}
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.