3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * IntlDateTime is an extended version of php 5 DateTime class with integrated * IntlDateFormatter functionality which adds support for multible calendars * and locales provided by ICU project. (needs php >= 5.3.0 with intl extension) * However, this class is not compatible with DateTime class because it uses ICU * pattern syntax for formatting and parsing date strings. * (@link http://userguide.icu-project.org/formatparse/datetime) * * @copyright Copyright 2010, Ali Farhadi (http://farhadi.ir/) * @license GNU General Public License 3.0 (http://www.gnu.org/licenses/gpl.html) */ class IntlDateTime extends DateTime { /** * @var string The current locale in use */ protected $locale; /** * @var string The current calendar in use */ protected $calendar; /** * Creates a new instance of IntlDateTime * * @param mixed $time Unix timestamp or strtotime() compatible string or another DateTime object * @param mixed $timezone DateTimeZone object or timezone identifier as full name (e.g. Asia/Tehran) or abbreviation (e.g. IRDT). * @param string $calendar any calendar supported by ICU (e.g. gregorian, persian, islamic, ...) * @param string $locale any locale supported by ICU * @param string $pattern the date pattern in which $time is formatted. * @return IntlDateTime */ public function __construct($time = null, $timezone = null, $calendar = 'gregorian', $locale = 'en_US', $pattern = null) { if (!isset($timezone)) $timezone = new DateTimeZone(date_default_timezone_get()); elseif (!is_a($timezone, 'DateTimeZone')) $timezone = new DateTimeZone($timezone); parent::__construct(null, $timezone); $this->setLocale($locale); $this->setCalendar($calendar); if (isset($time)) $this->set($time, null, $pattern); } /** * Returns an instance of IntlDateFormatter with specified options. * * @param array $options * @return IntlDateFormatter */ protected function getFormatter($options = array()) { $locale = empty($options['locale']) ? $this->locale : $options['locale']; $calendar = empty($options['calendar']) ? $this->calendar : $options['calendar']; $timezone = empty($options['timezone']) ? $this->getTimezone() : $options['timezone']; if (is_a($timezone, 'DateTimeZone')) $timezone = $timezone->getName(); $pattern = empty($options['pattern']) ? null : $options['pattern']; return new IntlDateFormatter($locale . '@calendar=' . $calendar, IntlDateFormatter::FULL, IntlDateFormatter::FULL, $timezone, $calendar == 'gregorian' ? IntlDateFormatter::GREGORIAN : IntlDateFormatter::TRADITIONAL, $pattern); } /** * Replaces localized digits in $str with latin digits. * * @param string $str * @return string Latinized string */ protected function latinizeDigits($str) { $result = ''; $num = new NumberFormatter($this->locale, NumberFormatter::DECIMAL); preg_match_all('/.[\x80-\xBF]*/', $str, $matches); foreach ($matches[0] as $char) { $pos = 0; $parsedChar = $num->parse($char, NumberFormatter::TYPE_INT32, $pos); $result .= $pos ? $parsedChar : $char; } return $result; } /** * Tries to guess the date pattern in which $time is formatted. * * @param string $time The date string * @return string Detected ICU pattern on success, FALSE otherwise. */ protected function guessPattern($time) { $time = $this->latinizeDigits(trim($time)); $shortDateRegex = '(\d{2,4})(-|\\\\|/)\d{1,2}\2\d{1,2}'; $longDateRegex = '([^\d]*\s)?\d{1,2}(-| )[^-\s\d]+\4(\d{2,4})'; $timeRegex = '\d{1,2}:\d{1,2}(:\d{1,2})?(\s.*)?'; if (preg_match("@^(?:(?:$shortDateRegex)|(?:$longDateRegex))(\s+$timeRegex)?$@", $time, $match)) { if (!empty($match[1])) { $separator = $match[2]; $pattern = strlen($match[1]) == 2 ? 'yy' : 'yyyy'; $pattern .= $separator . 'MM' . $separator . 'dd'; } else { $separator = $match[4]; $pattern = 'dd' . $separator . 'LLL' . $separator; $pattern .= strlen($match[5]) == 2 ? 'yy' : 'yyyy'; if (!empty($match[3])) $pattern = (preg_match('/,\s+$/', $match[3]) ? 'E, ' : 'E ') . $pattern; } if (!empty($match[6])) { $pattern .= !empty($match[8]) ? ' hh:mm' : ' HH:mm'; if (!empty($match[7])) $pattern .= ':ss'; if (!empty($match[8])) $pattern .= ' a'; } return $pattern; } return false; } /** * Sets the locale used by the object. * * @param string $locale * @return IntlDateTime The modified DateTime. */ public function setLocale($locale) { $this->locale = $locale; return $this; } /** * Gets the current locale used by the object. * * @return string */ public function getLocale() { return $this->locale; } /** * Sets the calendar used by the object. * * @param string $calendar * @return IntlDateTime The modified DateTime. */ public function setCalendar($calendar) { $this->calendar = strtolower($calendar); return $this; } /** * Gets the current calendar used by the object. * * @return string */ public function getCalendar() { return $this->calendar; } /** * Overrides the getTimestamp method to support timestamps out of the integer range. * * @return float Unix timestamp representing the date. */ public function getTimestamp() { return floatval(parent::format('U')); } /** * Overrides the setTimestamp method to support timestamps out of the integer range. * * @param float $unixtimestamp Unix timestamp representing the date. * @return IntlDateTime the modified DateTime. */ public function setTimestamp($unixtimestamp) { $diff = $unixtimestamp - $this->getTimestamp(); $days = floor($diff / 86400); $seconds = $diff - $days * 86400; $timezone = $this->getTimezone(); $this->setTimezone('UTC'); parent::modify("$days days $seconds seconds"); $this->setTimezone($timezone); return $this; } /** * Alters object's internal timestamp with a string acceptable by strtotime() or a Unix timestamp or a DateTime object. * * @param mixed $time Unix timestamp or strtotime() compatible string or another DateTime object * @param mixed $timezone DateTimeZone object or timezone identifier as full name (e.g. Asia/Tehran) or abbreviation (e.g. IRDT). * @param string $pattern the date pattern in which $time is formatted. * @return IntlDateTime The modified DateTime. */ public function set($time, $timezone = null, $pattern = null) { if (is_a($time, 'DateTime')) { $time = $time->format('U'); } elseif (!is_numeric($time) || $pattern) { if (!$pattern) { $pattern = $this->guessPattern($time); } if (!$pattern && preg_match('/((?:[+-]?\d+)|next|last|previous)\s*(year|month)s?/i', $time)) { if (isset($timezone)) { $tempTimezone = $this->getTimezone(); $this->setTimezone($timezone); } $this->setTimestamp(time()); $this->modify($time); if (isset($timezone)) { $this->setTimezone($tempTimezone); } return $this; } $timezone = empty($timezone) ? $this->getTimezone() : $timezone; if (is_a($timezone, 'DateTimeZone')) $timezone = $timezone->getName(); $defaultTimezone = @date_default_timezone_get(); date_default_timezone_set($timezone); if ($pattern) { $time = $this->getFormatter(array('timezone' => 'GMT', 'pattern' => $pattern))->parse($time); $time -= date('Z', $time); } else { $time = strtotime($time); } date_default_timezone_set($defaultTimezone); } $this->setTimestamp($time); return $this; } /** * Resets the current date of the object. * * @param integer $year * @param integer $month * @param integer $day * @return IntlDateTime The modified DateTime. */ public function setDate($year, $month, $day) { $this->set("$year/$month/$day ".$this->format('HH:mm:ss'), null, 'yyyy/MM/dd HH:mm:ss'); return $this; } /** * Sets the timezone for the object. * * @param mixed $timezone DateTimeZone object or timezone identifier as full name (e.g. Asia/Tehran) or abbreviation (e.g. IRDT). * @return IntlDateTime The modified DateTime. */ public function setTimezone($timezone) { if (!is_a($timezone, 'DateTimeZone')) $timezone = new DateTimeZone($timezone); parent::setTimezone($timezone); return $this; } /** * Internally used by modify method to calculate calendar-aware modifications * * @param array $matches * @return string An empty string */ protected function modifyCallback($matches) { if (!empty($matches[1])) { parent::modify($matches[1]); } list($y, $m, $d) = explode('-', $this->format('y-M-d')); $change = strtolower($matches[2]); $unit = strtolower($matches[3]); switch ($change) { case "next": $change = 1; break; case "last": case "previous": $change = -1; break; } switch ($unit) { case "month": $m += $change; if ($m > 12) { $y += floor($m/12); $m = $m % 12; } elseif ($m < 1) { $y += ceil($m/12) - 1; $m = $m % 12 + 12; } break; case "year": $y += $change; break; } $this->setDate($y, $m, $d); return ''; } /** * Alter the timestamp by incrementing or decrementing in a format accepted by strtotime(). * * @param string $modify a string in a relative format accepted by strtotime(). * @return IntlDateTime The modified DateTime. */ public function modify($modify) { $modify = $this->latinizeDigits(trim($modify)); $modify = preg_replace_callback('/(.*?)((?:[+-]?\d+)|next|last|previous)\s*(year|month)s?/i', array($this, 'modifyCallback'), $modify); if ($modify) parent::modify($modify); return $this; } /** * Returns date formatted according to given pattern. * * @param string $pattern Date pattern in ICU syntax (@link http://userguide.icu-project.org/formatparse/datetime) * @param mixed $timezone DateTimeZone object or timezone identifier as full name (e.g. Asia/Tehran) or abbreviation (e.g. IRDT). * @return string Formatted date on success or FALSE on failure. */ public function format($pattern, $timezone = null) { if (isset($timezone)) { $tempTimezone = $this->getTimezone(); $this->setTimezone($timezone); } // Timezones DST data in ICU are not as accurate as PHP. // So we get timezone offset from php and pass it to ICU. $result = $this->getFormatter(array( 'timezone' => 'GMT' . (parent::format('Z') ? parent::format('P') : ''), 'pattern' => $pattern ))->format($this->getTimestamp()); if (isset($timezone)) { $this->setTimezone($tempTimezone); } return $result; } /** * Preserve original DateTime::format functionality * * @param string $format Format accepted by date(). * @param mixed $timezone DateTimeZone object or timezone identifier as full name (e.g. Asia/Tehran) or abbreviation (e.g. IRDT). * @return string Formatted date on success or FALSE on failure. */ public function classicFormat($format, $timezone = null) { if (isset($timezone)) { $tempTimezone = $this->getTimezone(); $this->setTimezone($timezone); } $result = parent::format($format); if (isset($timezone)) { $this->setTimezone($tempTimezone); } return $result; } } $date = new IntlDateTime('now', 'Asia/Tehran', 'gregorian'); echo $date->getTimestamp();

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.90.0070.02526.77
8.3.80.0060.01320.00
8.3.70.0130.01621.93
8.3.60.0130.01620.11
8.3.50.0120.02123.65
8.3.40.0130.00320.87
8.3.30.0060.01021.11
8.3.20.0040.00422.05
8.3.10.0060.00325.14
8.3.00.0040.00420.86
8.2.210.0060.01226.77
8.2.200.0090.00920.02
8.2.190.0040.02521.75
8.2.180.0100.04219.90
8.2.170.0080.00822.96
8.2.160.0120.00322.13
8.2.150.0000.00924.18
8.2.140.0080.00024.66
8.2.130.0000.00826.16
8.2.120.0080.00023.12
8.2.110.0000.01021.57
8.2.100.0160.00720.31
8.2.90.0060.00320.79
8.2.80.0030.00620.25
8.2.70.0090.00620.00
8.2.60.0000.01220.05
8.2.50.0130.00320.13
8.2.40.0080.00020.56
8.2.30.0040.00420.69
8.2.20.0030.00622.07
8.2.10.0030.00620.30
8.2.00.0040.00420.43
8.1.290.0070.01130.84
8.1.280.0090.01925.92
8.1.270.0040.00425.59
8.1.260.0080.00026.35
8.1.250.0030.00628.09
8.1.240.0090.00923.85
8.1.230.0200.00320.70
8.1.220.0060.00320.04
8.1.210.0060.00319.91
8.1.200.0070.00419.62
8.1.190.0040.00419.98
8.1.180.0060.00319.78
8.1.170.0000.01420.63
8.1.160.0040.00423.68
8.1.150.0040.00420.82
8.1.140.0030.00620.02
8.1.130.0050.00320.06
8.1.120.0040.00420.18
8.1.110.0060.00320.08
8.1.100.0000.01020.05
8.1.90.0000.00819.88
8.1.80.0090.00020.14
8.1.70.0080.00020.17
8.1.60.0070.00419.93
8.1.50.0050.00519.94
8.1.40.0060.00320.20
8.1.30.0030.00620.29
8.1.20.0030.00620.38
8.1.10.0000.00820.09
8.1.00.0000.00920.05
8.0.300.0030.00620.90
8.0.290.0080.00019.25
8.0.280.0040.00420.27
8.0.270.0000.00819.45
8.0.260.0000.00819.37
8.0.250.0000.00819.53
8.0.240.0030.00619.59
8.0.230.0040.00419.45
8.0.220.0040.00419.56
8.0.210.0050.00319.29
8.0.200.0000.00719.37
8.0.190.0080.00019.48
8.0.180.0050.00519.36
8.0.170.0040.00419.39
8.0.160.0000.00819.29
8.0.150.0120.00819.20
8.0.140.0030.00519.30
8.0.130.0000.00713.82
8.0.120.0060.00319.54
8.0.110.0030.00619.59
8.0.100.0000.00819.50
8.0.90.0000.00919.61
8.0.80.0090.01219.63
8.0.70.0030.00519.49
8.0.60.0050.00319.63
8.0.50.0040.00419.45
8.0.30.0080.01320.01
8.0.20.0130.00720.01
8.0.10.0060.00319.89
8.0.00.0080.01419.52
7.4.330.0060.00015.19
7.4.320.0040.00419.28
7.4.300.0030.00319.39
7.4.290.0000.01019.37
7.4.280.0030.00619.34
7.4.270.0040.00419.22
7.4.260.0230.00419.16
7.4.250.0030.00519.23
7.4.240.0050.00319.39
7.4.230.0030.00519.17
7.4.220.0080.01219.28
7.4.210.0160.00719.32
7.4.200.0050.00319.47
7.4.160.0140.00319.02
7.4.150.0130.00819.21
7.4.140.0090.00918.96
7.4.130.0050.01419.21
7.4.120.0120.01019.01
7.4.110.0060.01319.04
7.4.100.0090.01819.17
7.4.90.0090.00919.39
7.4.80.0210.00319.39
7.4.70.0090.00919.35
7.4.60.0130.01919.18
7.4.50.0050.00019.32
7.4.40.0140.00319.43
7.4.30.0030.01419.21
7.4.00.0100.00317.62
7.3.330.0030.00313.72
7.3.320.0030.00313.70
7.3.310.0000.00919.13
7.3.300.0050.00218.88
7.3.290.0000.01619.09
7.3.280.0130.00819.08
7.3.270.0060.01519.09
7.3.260.0130.00719.01
7.3.250.0120.01419.17
7.3.240.0060.01619.05
7.3.230.0070.01119.42
7.3.210.0200.00719.32
7.3.200.0030.01419.39
7.3.190.0140.00519.32
7.3.180.0080.00919.01
7.3.170.0070.01119.24
7.3.160.0090.00919.23
7.3.120.0100.01017.22
7.3.10.0030.01318.91
7.3.00.0070.01018.60
7.2.330.0030.01619.37
7.2.320.0100.01019.27
7.2.310.0090.00919.31
7.2.300.0120.00619.48
7.2.290.0120.00619.40
7.2.130.0070.01119.10
7.2.120.0000.01619.01
7.2.110.0060.01219.11
7.2.100.0100.00619.02
7.2.90.0100.00319.05
7.2.80.0070.01019.17
7.2.70.0100.01319.27
7.2.60.0060.01119.39
7.2.50.0180.00319.34
7.2.40.0130.00719.02
7.2.30.0140.00619.23
7.2.20.0100.01019.11
7.2.10.0150.00419.15
7.2.00.0030.01419.14
7.1.250.0080.00818.31
7.1.200.0160.00818.30
7.1.100.0000.01721.25
7.1.70.0030.00920.00
7.1.60.0160.00920.20
7.1.50.0060.01919.72
7.1.00.0030.08024.56
7.0.200.0000.00819.27
7.0.140.0070.07324.26
7.0.100.0030.07722.02
7.0.90.0030.08022.02
7.0.80.0070.06722.12
7.0.70.0100.07322.08
7.0.60.0200.03322.19
7.0.50.0070.03722.49
7.0.40.0200.06021.91
7.0.30.0070.04321.85
7.0.20.0030.07022.00
7.0.10.0070.06322.03
7.0.00.0030.04722.10
5.6.280.0030.07323.27
5.6.250.0100.08322.63
5.6.240.0000.04322.63
5.6.230.0100.06022.63
5.6.220.0070.07722.61
5.6.210.0070.05022.53
5.6.200.0070.04022.97
5.6.190.0030.07722.82
5.6.180.0070.05722.74
5.6.170.0100.04723.07
5.6.160.0070.06022.82
5.6.150.0070.07322.73
5.6.140.0030.08722.87
5.6.130.0100.07022.87
5.6.120.0070.08723.09
5.6.110.0070.07022.72
5.6.100.0030.08022.55
5.6.90.0170.06722.86
5.6.80.0200.06022.31
5.6.70.0100.07322.24
5.6.60.0030.06022.34
5.6.50.0070.08022.25
5.6.40.0130.07022.10
5.6.30.0070.06022.38
5.6.20.0070.04022.32
5.6.10.0100.07022.45
5.6.00.0070.08022.13
5.5.380.0070.05322.39
5.5.370.0170.06022.55
5.5.360.0170.02722.38
5.5.350.0100.07322.44
5.5.340.0070.08322.75
5.5.330.0100.04322.75
5.5.320.0100.08022.47
5.5.310.0030.04322.87
5.5.300.0130.05322.44
5.5.290.0030.08022.74
5.5.280.0130.07322.61
5.5.270.0000.05722.70
5.5.260.0100.05022.43
5.5.250.0100.07022.25
5.5.240.0070.08021.84
5.5.230.0130.04021.95
5.5.220.0100.03722.20
5.5.210.0000.06021.90
5.5.200.0000.04722.12
5.5.190.0070.04021.77
5.5.180.0070.06021.88
5.5.160.0070.07021.87
5.5.150.0130.07021.76
5.5.140.0100.05321.91
5.5.130.0000.08322.25
5.5.120.0200.07321.84
5.5.110.0100.04321.81
5.5.100.0070.05721.93
5.5.90.0030.05722.00
5.5.80.0100.04722.16
5.5.70.0030.06721.87
5.5.60.0030.05021.86
5.5.50.0000.05322.09
5.5.40.0070.04721.63
5.5.30.0130.05021.63
5.5.20.0130.07021.62
5.5.10.0030.04721.76
5.5.00.0100.07721.73
5.4.450.0100.04321.05
5.4.440.0230.04721.25
5.4.430.0100.06321.05
5.4.420.0100.07721.07
5.4.410.0000.07721.09
5.4.400.0130.07720.78
5.4.390.0100.04320.89
5.4.380.0100.06720.89
5.4.370.0030.05720.98
5.4.360.0030.07720.88
5.4.350.0030.08020.98
5.4.340.0170.06720.61
5.4.320.0100.05720.88
5.4.310.0030.05020.61
5.4.300.0070.06720.73
5.4.290.0030.04320.89
5.4.280.0100.06720.88
5.4.270.0000.04320.96
5.4.260.0100.06720.92
5.4.250.0100.04320.73
5.4.240.0030.04020.88
5.4.230.0070.03720.82
5.4.220.0100.07320.75
5.4.210.0030.04020.87
5.4.200.0070.04720.72
5.4.190.0030.06720.70
5.4.180.0130.06020.87
5.4.170.0170.05020.71
5.4.160.0070.06320.71
5.4.150.0030.05320.91
5.4.140.0170.07018.00
5.4.130.0030.04318.21
5.4.120.0030.04718.27
5.4.110.0000.04718.09
5.4.100.0130.03018.25
5.4.90.0070.05718.32
5.4.80.0100.03718.23
5.4.70.0100.06018.07
5.4.60.0030.03718.21
5.4.50.0070.05018.22
5.4.40.0070.03318.25
5.4.30.0000.08718.21
5.4.20.0000.03718.21
5.4.10.0100.07018.23
5.4.00.0100.02717.61
5.3.290.0000.06014.88
5.3.280.0030.05014.81
5.3.270.0100.03314.68
5.3.260.0030.06714.73
5.3.250.0070.05714.63
5.3.240.0070.08014.81
5.3.230.0130.06714.68
5.3.220.0030.04314.69
5.3.210.0030.04714.59
5.3.200.0100.04714.62
5.3.190.0170.05714.79
5.3.180.0100.05314.58
5.3.170.0100.05314.62
5.3.160.0070.06014.64
5.3.150.0070.07714.72
5.3.140.0000.08014.66
5.3.130.0070.07314.62
5.3.120.0000.05314.60
5.3.110.0070.04714.63
5.3.100.0030.03714.25
5.3.90.0030.04014.14
5.3.80.0030.06714.01
5.3.70.0030.03714.27
5.3.60.0070.06014.15
5.3.50.0030.07313.93
5.3.40.0030.07314.18
5.3.30.0130.02713.90
5.3.20.0100.02713.92
5.3.10.0030.03713.89
5.3.00.0070.06013.84

preferences:
30.36 ms | 401 KiB | 5 Q