2015-07-11 01:29:12 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2017-03-22 19:16:35 +01:00
|
|
|
* Generates a list of available timezone continents and cities.
|
2015-07-11 01:29:12 +02:00
|
|
|
*
|
2017-03-22 19:16:35 +01:00
|
|
|
* Two distinct array based on available timezones
|
|
|
|
* and the one selected in the settings:
|
|
|
|
* - (0) continents:
|
|
|
|
* + list of available continents
|
|
|
|
* + special key 'selected' containing the value of the selected timezone's continent
|
|
|
|
* - (1) cities:
|
|
|
|
* + list of available cities associated with their continent
|
|
|
|
* + special key 'selected' containing the value of the selected timezone's city (without the continent)
|
2015-07-11 01:29:12 +02:00
|
|
|
*
|
2017-03-22 19:16:35 +01:00
|
|
|
* Example:
|
|
|
|
* [
|
|
|
|
* [
|
|
|
|
* 'America',
|
|
|
|
* 'Europe',
|
|
|
|
* 'selected' => 'Europe',
|
|
|
|
* ],
|
|
|
|
* [
|
|
|
|
* ['continent' => 'America', 'city' => 'Toronto'],
|
|
|
|
* ['continent' => 'Europe', 'city' => 'Paris'],
|
|
|
|
* 'selected' => 'Paris',
|
|
|
|
* ],
|
|
|
|
* ];
|
2015-07-11 01:29:12 +02:00
|
|
|
*
|
2017-03-22 19:16:35 +01:00
|
|
|
* Notes:
|
|
|
|
* - 'UTC/UTC' is mapped to 'UTC' to form a valid option
|
|
|
|
* - a few timezone cities includes the country/state, such as Argentina/Buenos_Aires
|
|
|
|
* - these arrays are designed to build timezone selects in template files with any HTML structure
|
|
|
|
*
|
|
|
|
* @param array $installedTimeZones List of installed timezones as string
|
2016-10-20 11:31:52 +02:00
|
|
|
* @param string $preselectedTimezone preselected timezone (optional)
|
2015-07-11 01:29:12 +02:00
|
|
|
*
|
2017-03-22 19:16:35 +01:00
|
|
|
* @return array[] continents and cities
|
2015-07-11 01:29:12 +02:00
|
|
|
**/
|
2017-03-22 19:16:35 +01:00
|
|
|
function generateTimeZoneData($installedTimeZones, $preselectedTimezone = '')
|
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
|
|
|
|
2017-03-22 19:16:35 +01:00
|
|
|
$continents = [];
|
|
|
|
$cities = [];
|
|
|
|
foreach ($installedTimeZones as $tz) {
|
2015-07-11 01:29:12 +02:00
|
|
|
if ($tz == 'UTC') {
|
|
|
|
$tz = 'UTC/UTC';
|
|
|
|
}
|
|
|
|
$spos = strpos($tz, '/');
|
|
|
|
|
2017-03-22 19:16:35 +01:00
|
|
|
// Ignore invalid timezones
|
|
|
|
if ($spos === false) {
|
|
|
|
continue;
|
2015-07-11 01:29:12 +02:00
|
|
|
}
|
|
|
|
|
2017-03-22 19:16:35 +01:00
|
|
|
$continent = substr($tz, 0, $spos);
|
|
|
|
$city = substr($tz, $spos+1);
|
|
|
|
$cities[] = ['continent' => $continent, 'city' => $city];
|
|
|
|
$continents[$continent] = true;
|
2015-07-11 01:29:12 +02:00
|
|
|
}
|
|
|
|
|
2017-03-22 19:16:35 +01:00
|
|
|
$continents = array_keys($continents);
|
|
|
|
$continents['selected'] = $pcontinent;
|
|
|
|
$cities['selected'] = $pcity;
|
2015-07-11 01:29:12 +02:00
|
|
|
|
2017-03-22 19:16:35 +01:00
|
|
|
return [$continents, $cities];
|
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()
|
|
|
|
);
|
|
|
|
}
|