3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class TimeZone { const NO_TIMEZONE = 0; const ASIA_YEKATERINBURG = 310; const EUROPE_KALININGRAD = 390; const EUROPE_MOSCOW = 401; const EUROPE_SAMARA = 409; const ROC = 492; const ROK = 493; const UCT = 494; const UTC = 495; const WET = 496; /** * list of time zones */ public static $constToFullName = array( self::ASIA_YEKATERINBURG => 'Asia/Yekaterinburg', self::EUROPE_KALININGRAD => 'Europe/Kaliningrad', self::EUROPE_MOSCOW => 'Europe/Moscow', self::EUROPE_SAMARA => 'Europe/Samara', ); /** * list of time zones indexed by name */ public static $fullNameToConst = array( 'Europe/Kaliningrad' => self::EUROPE_KALININGRAD, 'Europe/Moscow' => self::EUROPE_MOSCOW, 'Europe/Samara' => self::EUROPE_SAMARA, ); /** * Get time zone string based on constant value * * @param int $const * * @return string|null */ public static function get($const) { if (isset(static::$constToFullName[$const])) { return static::$constToFullName[$const]; } else { return null; } } /** * Get time zone constant value from passed time zone string * * @param int $code * * @return int|null */ public static function findByFullName($code) { if (isset(static::$fullNameToConst[$code])) { return static::$fullNameToConst[$code]; } else { return null; } } /** * Check if constant exists * * @param int $const * * @return bool */ public static function isConst($const) { return isset(static::$constToFullName[$const]); } /** * Get time zone constant value from passed time zone string * * @param string $timeZoneString * * @return bool */ public static function isTimeZone($timeZoneString) { return isset(static::$fullNameToConst[$timeZoneString]); } } const TIME_ZONE_FORMAT_NAME = 'utc'; $TIME_ZONE_FORMAT_NAME = 'utc'; $KEY_VALUE = 'value'; $KEY_NAME = 'name'; $KEY_OFFSET = 'offset'; $KEY_FORMAT = 'format'; $KEY_DISPLAY = 'display'; function convertTimezoneToArray($timezone, $value) { $dateTime = new \DateTime(); $dateTime->setTimeZone(new \DateTimeZone($timezone)); return [ $KEY_VALUE => $value, $KEY_NAME => $timezone, $KEY_OFFSET => $dateTime->format('P'), $KEY_FORMAT => $TIME_ZONE_FORMAT_NAME ]; } function convertTimezoneList(array $timezonesForCountry) { $timezoneArray = []; foreach ($timezonesForCountry as $timeZone) { if (TimeZone::isTimeZone($timeZone)) { $value = TimeZone::findByFullName($timeZone); $timezoneArray[] = convertTimezoneToArray($timeZone, $value); } } return $timezoneArray; } $inputArray = ["Asia/Yekaterinburg", "Europe/Kaliningrad", "Europe/Moscow", "Europe/Samara"]; convertTimezoneList($inputArray);
based on BWSXi
Output for 7.0.0 - 7.2.0
Notice: Undefined variable: KEY_VALUE in /in/K9OVU on line 127 Notice: Undefined variable: KEY_NAME in /in/K9OVU on line 128 Notice: Undefined variable: KEY_OFFSET in /in/K9OVU on line 129 Notice: Undefined variable: TIME_ZONE_FORMAT_NAME in /in/K9OVU on line 130 Notice: Undefined variable: KEY_FORMAT in /in/K9OVU on line 130 Notice: Undefined variable: KEY_VALUE in /in/K9OVU on line 127 Notice: Undefined variable: KEY_NAME in /in/K9OVU on line 128 Notice: Undefined variable: KEY_OFFSET in /in/K9OVU on line 129 Notice: Undefined variable: TIME_ZONE_FORMAT_NAME in /in/K9OVU on line 130 Notice: Undefined variable: KEY_FORMAT in /in/K9OVU on line 130 Notice: Undefined variable: KEY_VALUE in /in/K9OVU on line 127 Notice: Undefined variable: KEY_NAME in /in/K9OVU on line 128 Notice: Undefined variable: KEY_OFFSET in /in/K9OVU on line 129 Notice: Undefined variable: TIME_ZONE_FORMAT_NAME in /in/K9OVU on line 130 Notice: Undefined variable: KEY_FORMAT in /in/K9OVU on line 130
Output for 5.4.11 - 5.6.28
Notice: Undefined variable: KEY_VALUE in /in/K9OVU on line 127 Notice: Undefined variable: KEY_NAME in /in/K9OVU on line 128 Notice: Undefined variable: KEY_OFFSET in /in/K9OVU on line 129 Notice: Undefined variable: TIME_ZONE_FORMAT_NAME in /in/K9OVU on line 131 Notice: Undefined variable: KEY_FORMAT in /in/K9OVU on line 131 Notice: Undefined variable: KEY_VALUE in /in/K9OVU on line 127 Notice: Undefined variable: KEY_NAME in /in/K9OVU on line 128 Notice: Undefined variable: KEY_OFFSET in /in/K9OVU on line 129 Notice: Undefined variable: TIME_ZONE_FORMAT_NAME in /in/K9OVU on line 131 Notice: Undefined variable: KEY_FORMAT in /in/K9OVU on line 131 Notice: Undefined variable: KEY_VALUE in /in/K9OVU on line 127 Notice: Undefined variable: KEY_NAME in /in/K9OVU on line 128 Notice: Undefined variable: KEY_OFFSET in /in/K9OVU on line 129 Notice: Undefined variable: TIME_ZONE_FORMAT_NAME in /in/K9OVU on line 131 Notice: Undefined variable: KEY_FORMAT in /in/K9OVU on line 131
Output for 5.3.21 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/K9OVU on line 126
Process exited with code 255.