]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/TimeZone.php
Merge pull request #976 from ArthurHoaro/hotfix/url-parentheses
[github/shaarli/Shaarli.git] / application / TimeZone.php
CommitLineData
d1e2f8e5
V
1<?php
2/**
ae3aa968 3 * Generates a list of available timezone continents and cities.
d1e2f8e5 4 *
ae3aa968
A
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)
d1e2f8e5 13 *
ae3aa968
A
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 * ];
d1e2f8e5 27 *
ae3aa968
A
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
7af9a418 34 * @param string $preselectedTimezone preselected timezone (optional)
d1e2f8e5 35 *
ae3aa968 36 * @return array[] continents and cities
d1e2f8e5 37 **/
ae3aa968 38function generateTimeZoneData($installedTimeZones, $preselectedTimezone = '')
d1e2f8e5 39{
afd7b77b
V
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 }
d1e2f8e5 48
ae3aa968
A
49 $continents = [];
50 $cities = [];
51 foreach ($installedTimeZones as $tz) {
d1e2f8e5
V
52 if ($tz == 'UTC') {
53 $tz = 'UTC/UTC';
54 }
55 $spos = strpos($tz, '/');
56
ae3aa968
A
57 // Ignore invalid timezones
58 if ($spos === false) {
59 continue;
d1e2f8e5 60 }
d1e2f8e5 61
ae3aa968
A
62 $continent = substr($tz, 0, $spos);
63 $city = substr($tz, $spos+1);
64 $cities[] = ['continent' => $continent, 'city' => $city];
65 $continents[$continent] = true;
d1e2f8e5
V
66 }
67
ae3aa968
A
68 $continents = array_keys($continents);
69 $continents['selected'] = $pcontinent;
70 $cities['selected'] = $pcity;
d1e2f8e5 71
ae3aa968 72 return [$continents, $cities];
d1e2f8e5
V
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 *
7af9a418 83 * @return bool whether continent/city is a valid timezone
d1e2f8e5
V
84 */
85function isTimeZoneValid($continent, $city)
86{
d1e2f8e5
V
87 return in_array(
88 $continent.'/'.$city,
89 timezone_identifiers_list()
90 );
91}