2015-07-11 01:29:12 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Generates the timezone selection form and JavaScript.
|
|
|
|
*
|
|
|
|
* Note: 'UTC/UTC' is mapped to 'UTC' to form a valid option
|
|
|
|
*
|
|
|
|
* Example: preselect Europe/Paris
|
2015-08-04 18:31:16 +02:00
|
|
|
* list($htmlform, $js) = generateTimeZoneForm('Europe/Paris');
|
2015-07-11 01:29:12 +02:00
|
|
|
*
|
2016-10-20 11:31:52 +02:00
|
|
|
* @param string $preselectedTimezone preselected timezone (optional)
|
2015-07-11 01:29:12 +02:00
|
|
|
*
|
2016-10-20 11:31:52 +02:00
|
|
|
* @return array containing the generated HTML form and Javascript code
|
2015-07-11 01:29:12 +02:00
|
|
|
**/
|
2015-08-04 18:31:16 +02:00
|
|
|
function generateTimeZoneForm($preselectedTimezone='')
|
2015-07-11 01:29:12 +02:00
|
|
|
{
|
2015-08-04 18:31:16 +02:00
|
|
|
// Select the server timezone
|
|
|
|
if ($preselectedTimezone == '') {
|
|
|
|
$preselectedTimezone = date_default_timezone_get();
|
2015-07-11 01:29:12 +02:00
|
|
|
}
|
|
|
|
|
2015-08-04 18:31:16 +02:00
|
|
|
if ($preselectedTimezone == 'UTC') {
|
|
|
|
$pcity = $pcontinent = 'UTC';
|
|
|
|
} else {
|
|
|
|
// Try to split the provided timezone
|
|
|
|
$spos = strpos($preselectedTimezone, '/');
|
|
|
|
$pcontinent = substr($preselectedTimezone, 0, $spos);
|
|
|
|
$pcity = substr($preselectedTimezone, $spos+1);
|
|
|
|
}
|
2015-07-11 01:29:12 +02:00
|
|
|
|
2015-08-04 18:31:16 +02:00
|
|
|
// The list is in the form 'Europe/Paris', 'America/Argentina/Buenos_Aires'
|
2015-07-11 01:29:12 +02:00
|
|
|
// We split the list in continents/cities.
|
|
|
|
$continents = array();
|
|
|
|
$cities = array();
|
|
|
|
|
|
|
|
// TODO: use a template to generate the HTML/Javascript form
|
|
|
|
|
|
|
|
foreach (timezone_identifiers_list() as $tz) {
|
|
|
|
if ($tz == 'UTC') {
|
|
|
|
$tz = 'UTC/UTC';
|
|
|
|
}
|
|
|
|
$spos = strpos($tz, '/');
|
|
|
|
|
|
|
|
if ($spos !== false) {
|
|
|
|
$continent = substr($tz, 0, $spos);
|
|
|
|
$city = substr($tz, $spos+1);
|
|
|
|
$continents[$continent] = 1;
|
|
|
|
|
|
|
|
if (!isset($cities[$continent])) {
|
|
|
|
$cities[$continent] = '';
|
|
|
|
}
|
|
|
|
$cities[$continent] .= '<option value="'.$city.'"';
|
|
|
|
if ($pcity == $city) {
|
|
|
|
$cities[$continent] .= ' selected="selected"';
|
|
|
|
}
|
|
|
|
$cities[$continent] .= '>'.$city.'</option>';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-04 18:31:16 +02:00
|
|
|
$continentsHtml = '';
|
2015-07-11 01:29:12 +02:00
|
|
|
$continents = array_keys($continents);
|
|
|
|
|
|
|
|
foreach ($continents as $continent) {
|
2015-08-04 18:31:16 +02:00
|
|
|
$continentsHtml .= '<option value="'.$continent.'"';
|
2015-07-11 01:29:12 +02:00
|
|
|
if ($pcontinent == $continent) {
|
2015-08-04 18:31:16 +02:00
|
|
|
$continentsHtml .= ' selected="selected"';
|
2015-07-11 01:29:12 +02:00
|
|
|
}
|
2015-08-04 18:31:16 +02:00
|
|
|
$continentsHtml .= '>'.$continent.'</option>';
|
2015-07-11 01:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Timezone selection form
|
2015-08-04 18:31:16 +02:00
|
|
|
$timezoneForm = 'Continent:';
|
|
|
|
$timezoneForm .= '<select name="continent" id="continent" onChange="onChangecontinent();">';
|
|
|
|
$timezoneForm .= $continentsHtml.'</select>';
|
|
|
|
$timezoneForm .= ' City:';
|
|
|
|
$timezoneForm .= '<select name="city" id="city">'.$cities[$pcontinent].'</select><br />';
|
2015-07-11 01:29:12 +02:00
|
|
|
|
|
|
|
// Javascript handler - updates the city list when the user selects a continent
|
2015-08-04 18:31:16 +02:00
|
|
|
$timezoneJs = '<script>';
|
|
|
|
$timezoneJs .= 'function onChangecontinent() {';
|
|
|
|
$timezoneJs .= 'document.getElementById("city").innerHTML =';
|
|
|
|
$timezoneJs .= ' citiescontinent[document.getElementById("continent").value]; }';
|
|
|
|
$timezoneJs .= 'var citiescontinent = '.json_encode($cities).';';
|
|
|
|
$timezoneJs .= '</script>';
|
2015-07-11 01:29:12 +02:00
|
|
|
|
2015-08-04 18:31:16 +02:00
|
|
|
return array($timezoneForm, $timezoneJs);
|
2015-07-11 01:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tells if a continent/city pair form a valid timezone
|
|
|
|
*
|
|
|
|
* Note: 'UTC/UTC' is mapped to 'UTC'
|
|
|
|
*
|
|
|
|
* @param string $continent the timezone continent
|
|
|
|
* @param string $city the timezone city
|
|
|
|
*
|
2016-10-20 11:31:52 +02:00
|
|
|
* @return bool whether continent/city is a valid timezone
|
2015-07-11 01:29:12 +02:00
|
|
|
*/
|
|
|
|
function isTimeZoneValid($continent, $city)
|
|
|
|
{
|
|
|
|
return in_array(
|
|
|
|
$continent.'/'.$city,
|
|
|
|
timezone_identifiers_list()
|
|
|
|
);
|
|
|
|
}
|