]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/TimeZone.php
4 * Generates a list of available timezone continents and cities.
6 * Two distinct array based on available timezones
7 * and the one selected in the settings:
9 * + list of available continents
10 * + special key 'selected' containing the value of the selected timezone's continent
12 * + list of available cities associated with their continent
13 * + special key 'selected' containing the value of the selected timezone's city (without the continent)
20 * 'selected' => 'Europe',
23 * ['continent' => 'America', 'city' => 'Toronto'],
24 * ['continent' => 'Europe', 'city' => 'Paris'],
25 * 'selected' => 'Paris',
30 * - 'UTC/UTC' is mapped to 'UTC' to form a valid option
31 * - a few timezone cities includes the country/state, such as Argentina/Buenos_Aires
32 * - these arrays are designed to build timezone selects in template files with any HTML structure
34 * @param array $installedTimeZones List of installed timezones as string
35 * @param string $preselectedTimezone preselected timezone (optional)
37 * @return array[] continents and cities
39 function generateTimeZoneData($installedTimeZones, $preselectedTimezone = '')
41 if ($preselectedTimezone == 'UTC') {
42 $pcity = $pcontinent = 'UTC';
44 // Try to split the provided timezone
45 $spos = strpos($preselectedTimezone, '/');
46 $pcontinent = substr($preselectedTimezone, 0, $spos);
47 $pcity = substr($preselectedTimezone, $spos +
1);
52 foreach ($installedTimeZones as $tz) {
56 $spos = strpos($tz, '/');
58 // Ignore invalid timezones
59 if ($spos === false) {
63 $continent = substr($tz, 0, $spos);
64 $city = substr($tz, $spos +
1);
65 $cities[] = ['continent' => $continent, 'city' => $city];
66 $continents[$continent] = true;
69 $continents = array_keys($continents);
70 $continents['selected'] = $pcontinent;
71 $cities['selected'] = $pcity;
73 return [$continents, $cities];
77 * Tells if a continent/city pair form a valid timezone
79 * Note: 'UTC/UTC' is mapped to 'UTC'
81 * @param string $continent the timezone continent
82 * @param string $city the timezone city
84 * @return bool whether continent/city is a valid timezone
86 function isTimeZoneValid($continent, $city)
89 $continent . '/' . $city,
90 timezone_identifiers_list()