'GMT timezone', 'UTC' => 'UTC timezone', ]; /** * All continents of the world * * @var array */ protected static $continents = [ 'Africa' => DateTimeZone::AFRICA, 'America' => DateTimeZone::AMERICA, 'Antarctica' => DateTimeZone::ANTARCTICA, 'Arctic' => DateTimeZone::ARCTIC, 'Asia' => DateTimeZone::ASIA, 'Atlantic' => DateTimeZone::ATLANTIC, 'Australia' => DateTimeZone::AUSTRALIA, 'Europe' => DateTimeZone::EUROPE, 'Indian' => DateTimeZone::INDIAN, 'Pacific' => DateTimeZone::PACIFIC, ]; /** * Format to display timezones * * @param string $timezone * @param string $continent * @param bool $htmlencode * * @return string */ public static function formatTimezone($timezone, $continent, $htmlencode = true) { try { $time = new \DateTimeImmutable(null, new DateTimeZone($timezone)); } catch (\Exception $e) { Log::error($e->getMessage()); return ''; } $offset = $time->format('P'); if ($htmlencode) { $offset = str_replace(['-', '+'], [' − ', ' + '], $offset); } $timezone = substr($timezone, \strlen($continent) + 1); $timezone = str_replace(['St_', '_'], ['St. ', ' '], $timezone); return '(GMT/UTC'.$offset.')'.self::WHITESPACE_SEP.$timezone; } /** * Create a GMT timezone select element for form * * @param string $name * @param string $selected * @param mixed $attr * @param bool $htmlencode * * @throws \Exception * * @return string */ public static function create($name, $selected = '', $attr = '', $htmlencode = true) { // Attributes for select element $attrSet = ''; if (!empty($attr)) { if (\is_array($attr)) { foreach ($attr as $attr_name => $attr_value) { $attrSet .= ' '.$attr_name.'="'.$attr_value.'"'; } } else { $attrSet = ' '.$attr; } } // start select element $listbox = ''; return $listbox; } /** * Create a timezone array * * @param bool $htmlencode * * @return mixed */ public static function toArray($htmlencode = false) { $list = []; // Add popular timezones to list foreach (self::$popularTimezones as $key => $value) { $list['General'][$key] = $value; } // Add all timezone of continents to list foreach (self::$continents as $continent => $mask) { $timezones = DateTimeZone::listIdentifiers($mask); foreach ($timezones as $timezone) { $list[$continent][$timezone] = self::formatTimezone($timezone, $continent, $htmlencode); } } return $list; } }