Web development, scripts, source code and IT stuff
TimeZone select for PHP
Simple script that returns to us a list with possible TimeZones:
<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 ) ){ echo "<option>$value</option>"; } } ?> </select>
Improved version:
<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>
Be aware this will only work in PHP 5.2.0 or greater as they explain in the doc
Found this today, really useful and was exactly what I was looking for, so thanks.
I’ve added a couple of things, one to catch if it’s been posted and add the selected tag, and also I’ve moved the city structure around so it shows the citys when the are multiple sub citys (Argentina for eg)
Thanks again!
<?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 '’;
echo ”;
}
if(isset($ex[2])){
$city=$ex[1].”/”.$ex[2];
} else {
$city=$ex[1];
}
$continent=$ex[0];
echo ”.$city.”;
}
}
?>
nice! thanks!
[...] Long time a go I wrote about how to get timezones selector. [...]
Why no Africa or Australia? Antarctica is spelt wrongly. Apart from that I found it useful, thanks.
Hello,
The lists of timezones comes from: http://php.net/manual/en/datetimezone.listidentifiers.php
regards