wp_timezone_choice

Gives a nicely-formatted list of timezone strings.

Description

(string) wp_timezone_choice( (string) $selected_zone, (null) $locale = null ); 

Returns (string)

Parameters (2)

0. $selected_zone (string)
The selected zone.
1. $locale — Optional. (null) => null
Locale to load the timezones in. Default current site locale.

Usage

  1. if ( !function_exists( 'wp_timezone_choice' ) ) { 
  2. require_once ABSPATH . WPINC . '/functions.php'; 
  3.  
  4. // The selected zone. 
  5. $selected_zone = ''; 
  6.  
  7. // Optional. Locale to load the timezones in. Default current site locale. 
  8. $locale = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wp_timezone_choice($selected_zone, $locale); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/functions.php  
  1. function wp_timezone_choice( $selected_zone, $locale = null ) { 
  2. static $mo_loaded = false, $locale_loaded = null; 
  3.  
  4. $continents = array( 'Africa', 'America', 'Antarctica', 'Arctic', 'Asia', 'Atlantic', 'Australia', 'Europe', 'Indian', 'Pacific'); 
  5.  
  6. // Loadtranslationsfor continents and cities. 
  7. if ( ! $mo_loaded || $locale !== $locale_loaded ) { 
  8. $locale_loaded = $locale ? $locale : get_locale(); 
  9. $mofile = WP_LANG_DIR . '/continents-cities-' . $locale_loaded . '.mo'; 
  10. unload_textdomain( 'continents-cities' ); 
  11. load_textdomain( 'continents-cities', $mofile ); 
  12. $mo_loaded = true; 
  13.  
  14. $zonen = array(); 
  15. foreach ( timezone_identifiers_list() as $zone ) { 
  16. $zone = explode( '/', $zone ); 
  17. if ( !in_array( $zone[0], $continents ) ) { 
  18. continue; 
  19.  
  20. // This determines what gets set and translated - we don't translate Etc/* strings here, they are done later 
  21. $exists = array( 
  22. 0 => ( isset( $zone[0] ) && $zone[0] ),  
  23. 1 => ( isset( $zone[1] ) && $zone[1] ),  
  24. 2 => ( isset( $zone[2] ) && $zone[2] ),  
  25. ); 
  26. $exists[3] = ( $exists[0] && 'Etc' !== $zone[0] ); 
  27. $exists[4] = ( $exists[1] && $exists[3] ); 
  28. $exists[5] = ( $exists[2] && $exists[3] ); 
  29.  
  30. $zonen[] = array( 
  31. 'continent' => ( $exists[0] ? $zone[0] : '' ),  
  32. 'city' => ( $exists[1] ? $zone[1] : '' ),  
  33. 'subcity' => ( $exists[2] ? $zone[2] : '' ),  
  34. 't_continent' => ( $exists[3] ? translate( str_replace( '_', ' ', $zone[0] ), 'continents-cities' ) : '' ),  
  35. 't_city' => ( $exists[4] ? translate( str_replace( '_', ' ', $zone[1] ), 'continents-cities' ) : '' ),  
  36. 't_subcity' => ( $exists[5] ? translate( str_replace( '_', ' ', $zone[2] ), 'continents-cities' ) : '' ) 
  37. ); 
  38. usort( $zonen, '_wp_timezone_choice_usort_callback' ); 
  39.  
  40. $structure = array(); 
  41.  
  42. if ( empty( $selected_zone ) ) { 
  43. $structure[] = '<option selected="selected" value="">' . __( 'Select a city' ) . '</option>'; 
  44.  
  45. foreach ( $zonen as $key => $zone ) { 
  46. // Build value in an array to join later 
  47. $value = array( $zone['continent'] ); 
  48.  
  49. if ( empty( $zone['city'] ) ) { 
  50. // It's at the continent level (generally won't happen) 
  51. $display = $zone['t_continent']; 
  52. } else { 
  53. // It's inside a continent group 
  54.  
  55. // Continent optgroup 
  56. if ( !isset( $zonen[$key - 1] ) || $zonen[$key - 1]['continent'] !== $zone['continent'] ) { 
  57. $label = $zone['t_continent']; 
  58. $structure[] = '<optgroup label="'. esc_attr( $label ) .'">'; 
  59.  
  60. // Add the city to the value 
  61. $value[] = $zone['city']; 
  62.  
  63. $display = $zone['t_city']; 
  64. if ( !empty( $zone['subcity'] ) ) { 
  65. // Add the subcity to the value 
  66. $value[] = $zone['subcity']; 
  67. $display .= ' - ' . $zone['t_subcity']; 
  68.  
  69. // Build the value 
  70. $value = join( '/', $value ); 
  71. $selected = ''; 
  72. if ( $value === $selected_zone ) { 
  73. $selected = 'selected="selected" '; 
  74. $structure[] = '<option ' . $selected . 'value="' . esc_attr( $value ) . '">' . esc_html( $display ) . "</option>"; 
  75.  
  76. // Close continent optgroup 
  77. if ( !empty( $zone['city'] ) && ( !isset($zonen[$key + 1]) || (isset( $zonen[$key + 1] ) && $zonen[$key + 1]['continent'] !== $zone['continent']) ) ) { 
  78. $structure[] = '</optgroup>'; 
  79.  
  80. // Do UTC 
  81. $structure[] = '<optgroup label="'. esc_attr__( 'UTC' ) .'">'; 
  82. $selected = ''; 
  83. if ( 'UTC' === $selected_zone ) 
  84. $selected = 'selected="selected" '; 
  85. $structure[] = '<option ' . $selected . 'value="' . esc_attr( 'UTC' ) . '">' . __('UTC') . '</option>'; 
  86. $structure[] = '</optgroup>'; 
  87.  
  88. // Do manual UTC offsets 
  89. $structure[] = '<optgroup label="'. esc_attr__( 'Manual Offsets' ) .'">'; 
  90. $offset_range = array (-12, -11.5, -11, -10.5, -10, -9.5, -9, -8.5, -8, -7.5, -7, -6.5, -6, -5.5, -5, -4.5, -4, -3.5, -3, -2.5, -2, -1.5, -1, -0.5,  
  91. 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 5.75, 6, 6.5, 7, 7.5, 8, 8.5, 8.75, 9, 9.5, 10, 10.5, 11, 11.5, 12, 12.75, 13, 13.75, 14); 
  92. foreach ( $offset_range as $offset ) { 
  93. if ( 0 <= $offset ) 
  94. $offset_name = '+' . $offset; 
  95. else 
  96. $offset_name = (string) $offset; 
  97.  
  98. $offset_value = $offset_name; 
  99. $offset_name = str_replace(array('.25', '.5', '.75'), array(':15', ':30', ':45'), $offset_name); 
  100. $offset_name = 'UTC' . $offset_name; 
  101. $offset_value = 'UTC' . $offset_value; 
  102. $selected = ''; 
  103. if ( $offset_value === $selected_zone ) 
  104. $selected = 'selected="selected" '; 
  105. $structure[] = '<option ' . $selected . 'value="' . esc_attr( $offset_value ) . '">' . esc_html( $offset_name ) . "</option>"; 
  106.  
  107. $structure[] = '</optgroup>'; 
  108.  
  109. return join( "\n", $structure );