PHP List of Known Time Zones


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. function getTimezoneOptions() {
  4. $abbr = DateTimeZone::listAbbreviations();
  5. $options = array();
  6. foreach ($abbr as $section => $zones) {
  7. foreach ($zones as $zone) {
  8. if (!$zone['timezone_id']) {
  9. continue;
  10. }
  11. if (isset($options[$zone['timezone_id']])) {
  12. continue;
  13. }
  14. if (strpos($zone['timezone_id'], 'GMT') !== false) {
  15. // ignore the plain GMT zones
  16. continue;
  17. }
  18. $offset = round($zone['offset'], 2) / 3600;
  19. $hours = floor($offset);
  20. $minutes = ($offset - $hours) * 60;
  21. $minutes = $minutes == 0 ? '00' : $minutes;
  22. // ignore the weird ones
  23. if (!in_array($minutes, array('00','15','30','45'))) {
  24. continue;
  25. }
  26. $sign = substr($hours,0,1) == '-' ? '-' : '+';
  27. $hours = abs($hours);
  28. $options[$zone['timezone_id']] = str_replace('_', ' ', $zone['timezone_id']) . " ($sign$hours:$minutes)";
  29. }
  30. }
  31. asort($options);
  32. return $options;
  33. }
  34.  
  35. // example output:
  36. (
  37. [Africa/Abidjan] => Africa/Abidjan (+0:00)
  38. [Africa/Accra] => Africa/Accra (+0:00)
  39. [Africa/Addis_Ababa] => Africa/Addis Ababa (+3:00)
  40. [Africa/Algiers] => Africa/Algiers (+2:00)
  41. [Africa/Asmara] => Africa/Asmara (+3:00)
  42. ...
  43. )

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.