I\'ll try to explain what\'s the problem here.
According to list of supported timezones from PHP manual, I can see all valid TZ identifiers in PHP.
My first
Why not use @ operator? This code works pretty well, and you don't change default timezone:
function isValidTimezoneId($timezoneId) {
@$tz=timezone_open($timezoneId);
return $tz!==FALSE;
}
If you don't want @, you can do:
function isValidTimezoneId($timezoneId) {
try{
new DateTimeZone($timezoneId);
}catch(Exception $e){
return FALSE;
}
return TRUE;
}
When I tried this on a Linux system running 5.3.6, your Example #2 gave me 411 zones and Example #3 gave 496. The following slight modification to Example #2 gives me 591:
$zoneList = timezone_identifiers_list(DateTimeZone::ALL_WITH_BC);
There are no zones returned by Example #3 that are not returned with that modified Example #2.
On an OS X system running 5.3.3, Example #2 gives 407, Example #3 gives 564, and the modified Example #2 gives 565. Again, there are no zones returned by Example #3 that are not returned with that modified Example #2.
On a Linux system running 5.2.6 with the timezonedb PECL extension installed, Example #2 gives me 571 zones and Example #3 gives me only 488. There are no zones returned by Example #3 that are not by Example #2 on this system. The constant DateTimeZone::ALL_WITH_BC does not seem to exist in 5.2.6; it was probably added in 5.3.0.
So it seems the simplest way to get a list of all time zones in 5.3.x is
timezone_identifiers_list(DateTimeZone::ALL_WITH_BC)
, and in 5.2.x is timezone_identifiers_list()
. The simplest (if not fastest) way to check if a particular string is a valid time zone is still probably @timezone_open($timezoneId) !== false
.