]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/TimeZone.php
3 * Generates the timezone selection form and JavaScript.
5 * Note: 'UTC/UTC' is mapped to 'UTC' to form a valid option
7 * Example: preselect Europe/Paris
8 * list($htmlform, $js) = generateTimeZoneForm('Europe/Paris');
10 * @param string $preselected_timezone preselected timezone (optional)
12 * @return an array containing the generated HTML form and Javascript code
14 function generateTimeZoneForm($preselectedTimezone='')
16 // Select the server timezone
17 if ($preselectedTimezone == '') {
18 $preselectedTimezone = date_default_timezone_get();
21 if ($preselectedTimezone == 'UTC') {
22 $pcity = $pcontinent = 'UTC';
24 // Try to split the provided timezone
25 $spos = strpos($preselectedTimezone, '/');
26 $pcontinent = substr($preselectedTimezone, 0, $spos);
27 $pcity = substr($preselectedTimezone, $spos+
1);
30 // Display config form:
34 // The list is in the form 'Europe/Paris', 'America/Argentina/Buenos_Aires'
35 // We split the list in continents/cities.
36 $continents = array();
39 // TODO: use a template to generate the HTML/Javascript form
41 foreach (timezone_identifiers_list() as $tz) {
45 $spos = strpos($tz, '/');
47 if ($spos !== false) {
48 $continent = substr($tz, 0, $spos);
49 $city = substr($tz, $spos+
1);
50 $continents[$continent] = 1;
52 if (!isset($cities[$continent])) {
53 $cities[$continent] = '';
55 $cities[$continent] .= '<option value="'.$city.'"';
56 if ($pcity == $city) {
57 $cities[$continent] .= ' selected="selected"';
59 $cities[$continent] .= '>'.$city.'</option>';
64 $continents = array_keys($continents);
66 foreach ($continents as $continent) {
67 $continentsHtml .= '<option value="'.$continent.'"';
68 if ($pcontinent == $continent) {
69 $continentsHtml .= ' selected="selected"';
71 $continentsHtml .= '>'.$continent.'</option>';
74 // Timezone selection form
75 $timezoneForm = 'Continent:';
76 $timezoneForm .= '<select name="continent" id="continent" onChange="onChangecontinent();">';
77 $timezoneForm .= $continentsHtml.'</select>';
78 $timezoneForm .= ' City:';
79 $timezoneForm .= '<select name="city" id="city">'.$cities[$pcontinent].'</select><br />';
81 // Javascript handler - updates the city list when the user selects a continent
82 $timezoneJs = '<script>';
83 $timezoneJs .= 'function onChangecontinent() {';
84 $timezoneJs .= 'document.getElementById("city").innerHTML =';
85 $timezoneJs .= ' citiescontinent[document.getElementById("continent").value]; }';
86 $timezoneJs .= 'var citiescontinent = '.json_encode($cities).';';
87 $timezoneJs .= '</script>';
89 return array($timezoneForm, $timezoneJs);
93 * Tells if a continent/city pair form a valid timezone
95 * Note: 'UTC/UTC' is mapped to 'UTC'
97 * @param string $continent the timezone continent
98 * @param string $city the timezone city
100 * @return whether continent/city is a valid timezone
102 function isTimeZoneValid($continent, $city)
104 if ($continent == 'UTC' && $city == 'UTC') {
109 $continent.'/'.$city,
110 timezone_identifiers_list()