TimeZone.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Generates the timezone selection form and JavaScript.
  4. *
  5. * Note: 'UTC/UTC' is mapped to 'UTC' to form a valid option
  6. *
  7. * Example: preselect Europe/Paris
  8. * list($htmlform, $js) = generateTimeZoneForm('Europe/Paris');
  9. *
  10. * @param string $preselected_timezone preselected timezone (optional)
  11. *
  12. * @return an array containing the generated HTML form and Javascript code
  13. **/
  14. function generateTimeZoneForm($preselectedTimezone='')
  15. {
  16. // Select the server timezone
  17. if ($preselectedTimezone == '') {
  18. $preselectedTimezone = date_default_timezone_get();
  19. }
  20. if ($preselectedTimezone == 'UTC') {
  21. $pcity = $pcontinent = 'UTC';
  22. } else {
  23. // Try to split the provided timezone
  24. $spos = strpos($preselectedTimezone, '/');
  25. $pcontinent = substr($preselectedTimezone, 0, $spos);
  26. $pcity = substr($preselectedTimezone, $spos+1);
  27. }
  28. // Display config form:
  29. $timezoneForm = '';
  30. $timezoneJs = '';
  31. // The list is in the form 'Europe/Paris', 'America/Argentina/Buenos_Aires'
  32. // We split the list in continents/cities.
  33. $continents = array();
  34. $cities = array();
  35. // TODO: use a template to generate the HTML/Javascript form
  36. foreach (timezone_identifiers_list() as $tz) {
  37. if ($tz == 'UTC') {
  38. $tz = 'UTC/UTC';
  39. }
  40. $spos = strpos($tz, '/');
  41. if ($spos !== false) {
  42. $continent = substr($tz, 0, $spos);
  43. $city = substr($tz, $spos+1);
  44. $continents[$continent] = 1;
  45. if (!isset($cities[$continent])) {
  46. $cities[$continent] = '';
  47. }
  48. $cities[$continent] .= '<option value="'.$city.'"';
  49. if ($pcity == $city) {
  50. $cities[$continent] .= ' selected="selected"';
  51. }
  52. $cities[$continent] .= '>'.$city.'</option>';
  53. }
  54. }
  55. $continentsHtml = '';
  56. $continents = array_keys($continents);
  57. foreach ($continents as $continent) {
  58. $continentsHtml .= '<option value="'.$continent.'"';
  59. if ($pcontinent == $continent) {
  60. $continentsHtml .= ' selected="selected"';
  61. }
  62. $continentsHtml .= '>'.$continent.'</option>';
  63. }
  64. // Timezone selection form
  65. $timezoneForm = 'Continent:';
  66. $timezoneForm .= '<select name="continent" id="continent" onChange="onChangecontinent();">';
  67. $timezoneForm .= $continentsHtml.'</select>';
  68. $timezoneForm .= '&nbsp;&nbsp;&nbsp;&nbsp;City:';
  69. $timezoneForm .= '<select name="city" id="city">'.$cities[$pcontinent].'</select><br />';
  70. // Javascript handler - updates the city list when the user selects a continent
  71. $timezoneJs = '<script>';
  72. $timezoneJs .= 'function onChangecontinent() {';
  73. $timezoneJs .= 'document.getElementById("city").innerHTML =';
  74. $timezoneJs .= ' citiescontinent[document.getElementById("continent").value]; }';
  75. $timezoneJs .= 'var citiescontinent = '.json_encode($cities).';';
  76. $timezoneJs .= '</script>';
  77. return array($timezoneForm, $timezoneJs);
  78. }
  79. /**
  80. * Tells if a continent/city pair form a valid timezone
  81. *
  82. * Note: 'UTC/UTC' is mapped to 'UTC'
  83. *
  84. * @param string $continent the timezone continent
  85. * @param string $city the timezone city
  86. *
  87. * @return whether continent/city is a valid timezone
  88. */
  89. function isTimeZoneValid($continent, $city)
  90. {
  91. return in_array(
  92. $continent.'/'.$city,
  93. timezone_identifiers_list()
  94. );
  95. }