Advertisement
nathan15

Timezone List Function (from Stack Overflow)

May 30th, 2014
1,269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.52 KB | None | 0 0
  1. // Modified version of the timezone list function from http://stackoverflow.com/a/17355238/507629
  2. // Includes current time for each timezone (would help users who don't know what their timezone is)
  3.  
  4. function generate_timezone_list()
  5. {
  6.     static $regions = array(
  7.         DateTimeZone::AFRICA,
  8.         DateTimeZone::AMERICA,
  9.         DateTimeZone::ANTARCTICA,
  10.         DateTimeZone::ASIA,
  11.         DateTimeZone::ATLANTIC,
  12.         DateTimeZone::AUSTRALIA,
  13.         DateTimeZone::EUROPE,
  14.         DateTimeZone::INDIAN,
  15.         DateTimeZone::PACIFIC,
  16.     );
  17.  
  18.     $timezones = array();
  19.     foreach( $regions as $region )
  20.     {
  21.         $timezones = array_merge( $timezones, DateTimeZone::listIdentifiers( $region ) );
  22.     }
  23.  
  24.     $timezone_offsets = array();
  25.     foreach( $timezones as $timezone )
  26.     {
  27.         $tz = new DateTimeZone($timezone);
  28.         $timezone_offsets[$timezone] = $tz->getOffset(new DateTime);
  29.     }
  30.  
  31.     // sort timezone by timezone name
  32.     ksort($timezone_offsets);
  33.  
  34.     $timezone_list = array();
  35.     foreach( $timezone_offsets as $timezone => $offset )
  36.     {
  37.         $offset_prefix = $offset < 0 ? '-' : '+';
  38.         $offset_formatted = gmdate( 'H:i', abs($offset) );
  39.  
  40.         $pretty_offset = "UTC${offset_prefix}${offset_formatted}";
  41.        
  42.         $t = new DateTimeZone($timezone);
  43.         $c = new DateTime(null, $t);
  44.         $current_time = $c->format('g:i A');
  45.  
  46.         $timezone_list[$timezone] = "(${pretty_offset}) $timezone - $current_time";
  47.     }
  48.  
  49.     return $timezone_list;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement