]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/TimeZone.php
SessionManager+LoginManager: fix checkLoginState logic
[github/shaarli/Shaarli.git] / application / TimeZone.php
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
49 $continents = [];
50 $cities = [];
51 foreach ($installedTimeZones as $tz) {
52 if ($tz == 'UTC') {
53 $tz = 'UTC/UTC';
54 }
55 $spos = strpos($tz, '/');
56
57 // Ignore invalid timezones
58 if ($spos === false) {
59 continue;
60 }
61
62 $continent = substr($tz, 0, $spos);
63 $city = substr($tz, $spos+1);
64 $cities[] = ['continent' => $continent, 'city' => $city];
65 $continents[$continent] = true;
66 }
67
68 $continents = array_keys($continents);
69 $continents['selected'] = $pcontinent;
70 $cities['selected'] = $pcity;
71
72 return [$continents, $cities];
73 }
74
75 /**
76 * Tells if a continent/city pair form a valid timezone
77 *
78 * Note: 'UTC/UTC' is mapped to 'UTC'
79 *
80 * @param string $continent the timezone continent
81 * @param string $city the timezone city
82 *
83 * @return bool whether continent/city is a valid timezone
84 */
85 function isTimeZoneValid($continent, $city)
86 {
87 return in_array(
88 $continent.'/'.$city,
89 timezone_identifiers_list()
90 );
91 }