You are here

function system_time_zones in Drupal 8

Same name and namespace in other branches
  1. 7 modules/system/system.module \system_time_zones()
  2. 9 core/modules/system/system.module \system_time_zones()

Generate an array of time zones and their local time&date.

Parameters

mixed $blank: If evaluates true, prepend an empty time zone option to the array.

bool $grouped: (optional) Whether the timezones should be grouped by region.

Return value

array An array or nested array containing time zones, keyed by the system name.

10 calls to system_time_zones()
AccountForm::form in core/modules/user/src/AccountForm.php
Gets the actual form array to be built.
Date::buildOptionsForm in core/modules/views/src/Plugin/views/field/Date.php
Default options form that provides the label widget that all fields should have.
DateTimeFormatterBase::settingsForm in core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php
Returns a form to configure settings for the formatter.
RegionalForm::buildForm in core/modules/system/src/Form/RegionalForm.php
Form constructor.
SiteConfigureForm::buildForm in core/lib/Drupal/Core/Installer/Form/SiteConfigureForm.php
Form constructor.

... See full list

File

core/modules/system/system.module, line 1262
Configuration system that lets administrators modify the workings of the site.

Code

function system_time_zones($blank = NULL, $grouped = FALSE) {
  $zonelist = timezone_identifiers_list();
  $zones = $blank ? [
    '' => t('- None selected -'),
  ] : [];
  foreach ($zonelist as $zone) {

    // Because many time zones exist in PHP only for backward compatibility
    // reasons and should not be used, the list is filtered by a regular
    // expression.
    if (preg_match('!^((Africa|America|Antarctica|Arctic|Asia|Atlantic|Australia|Europe|Indian|Pacific)/|UTC$)!', $zone)) {
      $zones[$zone] = t('@zone', [
        '@zone' => t(str_replace('_', ' ', $zone)),
      ]);
    }
  }

  // Sort the translated time zones alphabetically.
  asort($zones);
  if ($grouped) {
    $grouped_zones = [];
    foreach ($zones as $key => $value) {
      $split = explode('/', $value);
      $city = array_pop($split);
      $region = array_shift($split);
      if (!empty($region)) {
        $grouped_zones[$region][$key] = empty($split) ? $city : $city . ' (' . implode('/', $split) . ')';
      }
      else {
        $grouped_zones[$key] = $value;
      }
    }
    foreach ($grouped_zones as $key => $value) {
      if (is_array($grouped_zones[$key])) {
        asort($grouped_zones[$key]);
      }
    }
    $zones = $grouped_zones;
  }
  return $zones;
}