TimeZone.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 $preselectedTimezone preselected timezone (optional)
  11. *
  12. * @return 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. // The list is in the form 'Europe/Paris', 'America/Argentina/Buenos_Aires'
  29. // We split the list in continents/cities.
  30. $continents = array();
  31. $cities = array();
  32. // TODO: use a template to generate the HTML/Javascript form
  33. foreach (timezone_identifiers_list() as $tz) {
  34. if ($tz == 'UTC') {
  35. $tz = 'UTC/UTC';
  36. }
  37. $spos = strpos($tz, '/');
  38. if ($spos !== false) {
  39. $continent = substr($tz, 0, $spos);
  40. $city = substr($tz, $spos+1);
  41. $continents[$continent] = 1;
  42. if (!isset($cities[$continent])) {
  43. $cities[$continent] = '';
  44. }
  45. $cities[$continent] .= '<option value="'.$city.'"';
  46. if ($pcity == $city) {
  47. $cities[$continent] .= ' selected="selected"';
  48. }
  49. $cities[$continent] .= '>'.$city.'</option>';
  50. }
  51. }
  52. $continentsHtml = '';
  53. $continents = array_keys($continents);
  54. foreach ($continents as $continent) {
  55. $continentsHtml .= '<option value="'.$continent.'"';
  56. if ($pcontinent == $continent) {
  57. $continentsHtml .= ' selected="selected"';
  58. }
  59. $continentsHtml .= '>'.$continent.'</option>';
  60. }
  61. // Timezone selection form
  62. $timezoneForm = 'Continent:';
  63. $timezoneForm .= '<select name="continent" id="continent" onChange="onChangecontinent();">';
  64. $timezoneForm .= $continentsHtml.'</select>';
  65. $timezoneForm .= '&nbsp;&nbsp;&nbsp;&nbsp;City:';
  66. $timezoneForm .= '<select name="city" id="city">'.$cities[$pcontinent].'</select><br />';
  67. // Javascript handler - updates the city list when the user selects a continent
  68. $timezoneJs = '<script>';
  69. $timezoneJs .= 'function onChangecontinent() {';
  70. $timezoneJs .= 'document.getElementById("city").innerHTML =';
  71. $timezoneJs .= ' citiescontinent[document.getElementById("continent").value]; }';
  72. $timezoneJs .= 'var citiescontinent = '.json_encode($cities).';';
  73. $timezoneJs .= '</script>';
  74. return array($timezoneForm, $timezoneJs);
  75. }
  76. /**
  77. * Tells if a continent/city pair form a valid timezone
  78. *
  79. * Note: 'UTC/UTC' is mapped to 'UTC'
  80. *
  81. * @param string $continent the timezone continent
  82. * @param string $city the timezone city
  83. *
  84. * @return bool whether continent/city is a valid timezone
  85. */
  86. function isTimeZoneValid($continent, $city)
  87. {
  88. return in_array(
  89. $continent.'/'.$city,
  90. timezone_identifiers_list()
  91. );
  92. }