TimeZone.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * Generates a list of available timezone continents and cities.
  4. *
  5. * Two distinct array based on available timezones
  6. * and the one selected in the settings:
  7. * - (0) continents:
  8. * + list of available continents
  9. * + special key 'selected' containing the value of the selected timezone's continent
  10. * - (1) cities:
  11. * + list of available cities associated with their continent
  12. * + special key 'selected' containing the value of the selected timezone's city (without the continent)
  13. *
  14. * Example:
  15. * [
  16. * [
  17. * 'America',
  18. * 'Europe',
  19. * 'selected' => 'Europe',
  20. * ],
  21. * [
  22. * ['continent' => 'America', 'city' => 'Toronto'],
  23. * ['continent' => 'Europe', 'city' => 'Paris'],
  24. * 'selected' => 'Paris',
  25. * ],
  26. * ];
  27. *
  28. * Notes:
  29. * - 'UTC/UTC' is mapped to 'UTC' to form a valid option
  30. * - a few timezone cities includes the country/state, such as Argentina/Buenos_Aires
  31. * - these arrays are designed to build timezone selects in template files with any HTML structure
  32. *
  33. * @param array $installedTimeZones List of installed timezones as string
  34. * @param string $preselectedTimezone preselected timezone (optional)
  35. *
  36. * @return array[] continents and cities
  37. **/
  38. function generateTimeZoneData($installedTimeZones, $preselectedTimezone = '')
  39. {
  40. if ($preselectedTimezone == 'UTC') {
  41. $pcity = $pcontinent = 'UTC';
  42. } else {
  43. // Try to split the provided timezone
  44. $spos = strpos($preselectedTimezone, '/');
  45. $pcontinent = substr($preselectedTimezone, 0, $spos);
  46. $pcity = substr($preselectedTimezone, $spos+1);
  47. }
  48. $continents = [];
  49. $cities = [];
  50. foreach ($installedTimeZones as $tz) {
  51. if ($tz == 'UTC') {
  52. $tz = 'UTC/UTC';
  53. }
  54. $spos = strpos($tz, '/');
  55. // Ignore invalid timezones
  56. if ($spos === false) {
  57. continue;
  58. }
  59. $continent = substr($tz, 0, $spos);
  60. $city = substr($tz, $spos+1);
  61. $cities[] = ['continent' => $continent, 'city' => $city];
  62. $continents[$continent] = true;
  63. }
  64. $continents = array_keys($continents);
  65. $continents['selected'] = $pcontinent;
  66. $cities['selected'] = $pcity;
  67. return [$continents, $cities];
  68. }
  69. /**
  70. * Tells if a continent/city pair form a valid timezone
  71. *
  72. * Note: 'UTC/UTC' is mapped to 'UTC'
  73. *
  74. * @param string $continent the timezone continent
  75. * @param string $city the timezone city
  76. *
  77. * @return bool whether continent/city is a valid timezone
  78. */
  79. function isTimeZoneValid($continent, $city)
  80. {
  81. return in_array(
  82. $continent.'/'.$city,
  83. timezone_identifiers_list()
  84. );
  85. }