Reputation: 21
I have a bit of code that populates a selectbox with timezones however it includes Asia, Africa, etc. I would like to only have it list US timezones. Is that possible? The code I have now is...
/*
Get all timezone listing
*/
function event_tz_list() {
$zones_array = array();
$timestamp = time();
foreach(timezone_identifiers_list() as $key => $zone) {
date_default_timezone_set($zone);
$zones_array[$key]['zone'] = $zone;
$zones_array[$key]['offset'] = date('P', $timestamp);
$zones_array[$key]['diff_from_GMT'] = 'UTC/GMT ' . date('P',
$timestamp);
}
return $zones_array;
}
Upvotes: 1
Views: 353
Reputation: 78984
timezone_identifiers_list()
takes two arguments. A constant for what timezones to get and a two letter country code if the first argument is DateTimeZone::PER_COUNTRY
. So this will pretty much get it:
timezone_identifiers_list(DateTimeZone::PER_COUNTRY, 'US')
If you want to get the 7 abbreviations instead of the 29 America/City
values:
function event_tz_list() {
foreach(timezone_identifiers_list(DateTimeZone::PER_COUNTRY, 'US') as $key => $zone) {
$timestamp = time();
date_default_timezone_set($zone);
$abbrev = date('T'); // use as the key and you won't get duplicates
$zones_array[$abbrev]['zone'] = $abbrev;
$zones_array[$abbrev]['offset'] = date('P', $timestamp);
$zones_array[$abbrev]['diff_from_GMT'] = 'UTC/GMT ' . date('P', $timestamp);
}
return $zones_array;
}
print_r(event_tz_list());
Yields:
Array
(
[HAST] => Array
(
[zone] => HAST
[offset] => -10:00
[diff_from_GMT] => UTC/GMT -10:00
)
[AKST] => Array
(
[zone] => AKST
[offset] => -09:00
[diff_from_GMT] => UTC/GMT -09:00
)
[MST] => Array
(
[zone] => MST
[offset] => -07:00
[diff_from_GMT] => UTC/GMT -07:00
)
[CST] => Array
(
[zone] => CST
[offset] => -06:00
[diff_from_GMT] => UTC/GMT -06:00
)
[EST] => Array
(
[zone] => EST
[offset] => -05:00
[diff_from_GMT] => UTC/GMT -05:00
)
[PST] => Array
(
[zone] => PST
[offset] => -08:00
[diff_from_GMT] => UTC/GMT -08:00
)
[HST] => Array
(
[zone] => HST
[offset] => -10:00
[diff_from_GMT] => UTC/GMT -10:00
)
)
Which shows 7 timezones (I learned something):
There is no time difference between Hawaii-Aleutian Standard Time (HAST) and Hawaii Standard Time (HST).
Upvotes: 1