3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace Fisharebest\ExtCalendar; /** * interface CalendarInterface - each calendar implementation needs to provide * these methods. * * @author Greg Roach <fisharebest@gmail.com> * @copyright (c) 2014 Greg Roach * @license This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ interface CalendarInterface { public function daysInMonth($year, $month); public function dayOfWeek($jd); public function jdToYmd($jd); public function leapYear($year); public function ymdToJd($year, $month, $day); } /** * class Calendar - generic base class for specific calendars. * * @author Greg Roach <fisharebest@gmail.com> * @copyright (c) 2014 Greg Roach * @license This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ abstract class Calendar { /** Same as PHP’s ext/calendar extension */ const PHP_CALENDAR_NAME = 'Undefined'; /** Same as PHP’s ext/calendar extension */ const PHP_CALENDAR_NUMBER = -1; /** Same as PHP’s ext/calendar extension */ const PHP_CALENDAR_SYMBOL = 'CAL_UNDEFINED'; /** See the GEDCOM specification */ const GEDCOM_CALENDAR_ESCAPE = '@#DUNKNOWN@'; /** The earliest Julian Day number that can be converted into this calendar. */ const JD_START = 1; /** The latest Julian Day number that can be converted into this calendar. */ const JD_END = 2147483647; /** The maximum number of months in any year */ const MAX_MONTHS_IN_YEAR = 12; /** The maximum number of days in any month */ const MAX_DAYS_IN_MONTH = 31; /** * The concrete class needs to provide the implementation. * * @return string[] */ protected abstract function monthNames(); /** * English names for the days of the week. * * @return string[] */ protected function dayNames() { return array( 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', ); } /** * Abbreviated English names for the days of the week. * * @return string[] */ protected function dayNamesAbbreviated() { return array( 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', ); } /** * Convert a Julian Day number into a calendar date. * * @param $jd * @return int[] Array of month, day and year */ public function calFromJd($jd) { $dow = $this->dayOfWeek($jd); if ($jd >= static::JD_START && $jd <= static::JD_END) { list($year, $month, $day) = $this->jdToYmd($jd); return array( 'date' => $month . '/' . $day . '/' . $year, 'month' => $month, 'day' => $day, 'year' => $year, 'dow' => $dow, 'abbrevdayname' => $this->dayNameAbbreviated($dow), 'dayname' => $this->dayName($dow), 'abbrevmonth' => $this->jdMonthNameAbbreviated($jd), 'monthname' => $this->jdMonthName($jd), ); } else { return array( 'date' => '0/0/0', 'month' => 0, 'day' => 0, 'year' => 0, 'dow' => $dow, 'abbrevdayname' => $this->dayNameAbbreviated($dow), 'dayname' => $this->dayName($dow), 'abbrevmonth' => '', 'monthname' => '', ); } } /** * Provide information about this calendar. * * @return array */ public function phpCalInfo() { return array( 'months' => $this->monthNames(), 'abbrevmonths' => $this->monthNamesAbbreviated(), 'maxdaysinmonth' => static::MAX_DAYS_IN_MONTH, 'calname' => static::PHP_CALENDAR_NAME, 'calsymbol' => static::PHP_CALENDAR_SYMBOL, ); } /** * Calculate the day of the week for a given Julian Day number. * * @param int $jd * * @return int 0=Sunday ... 6=Saturday */ public function dayOfWeek($jd) { $dow = ($jd + 1) % 7; if ($dow < 0) { return $dow + 7; } else { return $dow; } } /** * English name for a day of the week. * * @param int $dow Day of the week * * @return string */ public function dayName($dow) { $days = $this->dayNames(); return $days[$dow]; } /** * Abbreviated English name for a day of the week. * * @param int $dow Day of the week * * @return string */ public function dayNameAbbreviated($dow) { $days = $this->dayNamesAbbreviated(); return $days[$dow]; } /** * Calculate the name of a month, for a specified Julian Day number. * * @param $jd * * @return string */ public function jdMonthName($jd) { list(, $month) = $this->jdToYmd($jd); $months = $this->monthNames(); return $months[$month]; } /** * Calculate the name of a month, for a specified Julian Day number. * * @param int $jd * * @return string */ public function jdMonthNameAbbreviated($jd) { list(, $month) = $this->jdToYmd($jd); $months = $this->monthNamesAbbreviated(); return $months[$month]; } /** * Unless otherwise defined, abbreviated month names are the same as full names. * * @return string[] */ public function monthNamesAbbreviated() { return $this->monthNames(); } } /** * class JulianCalendar - calculations for the Julian calendar. * * @author Greg Roach <fisharebest@gmail.com> * @copyright (c) 2014 Greg Roach * @license This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ class JulianCalendar extends Calendar implements CalendarInterface { /** Same as PHP’s ext/calendar extension */ const PHP_CALENDAR_NAME = 'Julian'; /** Same as PHP’s ext/calendar extension */ const PHP_CALENDAR_NUMBER = 1; /** Same as PHP’s ext/calendar extension */ const PHP_CALENDAR_SYMBOL = 'CAL_JULIAN'; /** See the GEDCOM specification */ const GEDCOM_CALENDAR_ESCAPE = '@#DJULIAN@'; /** * Month lengths for regular years and leap-years. * * @var int[][] */ protected static $DAYS_IN_MONTH = array( 0 => array(1 => 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31), 1 => array(1 => 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31), ); /** * English month names. * * @return string[] */ public function monthNames() { return array( 1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December', ); } /** * Abbreviated English month names. * * @return string[] */ public function monthNamesAbbreviated() { return array( 1 => 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', ); } /** * Determine whether a year is a leap year. * * @param int $year * @return bool */ public function leapYear($year) { if ($year < 0) { $year++; } return $year % 4 == 0; } /** * Convert a Julian day number into a year/month/day. * * @param $jd * * @return int[]; */ public function jdToYmd($jd) { $c = $jd + 32082; $d = (int)((4 * $c + 3) / 1461); $e = $c - (int)(1461 * $d / 4); $m = (int)((5 * $e + 2) / 153); $day = $e - (int)((153 * $m + 2) / 5) + 1; $month = $m + 3 - 12 * (int)($m / 10); $year = $d - 4800 + (int)($m / 10); if ($year < 1) { // 0=1BC, -1=2BC, etc. $year--; } return array($year, $month, $day); } /** * Convert a year/month/day into a Julian day number * * @param int $year * @param int $month * @param int $day * * @return int */ public function ymdToJd($year, $month, $day) { if ($year < 0) { // 1 B.C.E. => 0, 2 B.C.E> => 1, etc. ++$year; } $a = (int)((14 - $month) / 12); $year = $year + 4800 - $a; $month = $month + 12 * $a - 3; return $day + (int)((153 * $month + 2) / 5) + 365 * $year + (int)($year / 4) - 32083; } /** * Calculate the number of days in a month. * * @param int $year * @param int $month * * @return int */ public function daysInMonth($year, $month) { if ($year == 0 || $month < 1 || $month > 12) { return trigger_error('invalid date.', E_USER_WARNING); } else { return static::$DAYS_IN_MONTH[$this->leapYear($year)][$month]; } } /** * Get the number of days after March 21 that easter falls, for a given year. * * Uses the algorithm found in PHP’s ext/calendar/easter.c * * @param int $year * * @return int */ public function easterDays($year) { // The “golden” number $golden = 1 + $year % 19; // The “dominical” number (finding a Sunday) $dom = ($year + (int)($year / 4) + 5) % 7; if ($dom < 0) { $dom += 7; } // The uncorrected “Paschal full moon” date $pfm = (3 - 11 * $golden - 7) % 30; if ($pfm < 0) { $pfm += 30; } // The corrected “Paschal full moon” date if ($pfm == 29 || $pfm == 28 && $golden > 11) { $pfm--; } $tmp = (4 - $pfm - $dom) % 7; if ($tmp < 0) { $tmp += 7; } return $pfm + $tmp + 1; } }

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.60.0150.00018.29
8.3.50.0150.00622.83
8.3.40.0060.01218.54
8.3.30.0110.00418.96
8.3.20.0080.00020.16
8.3.10.0030.00523.65
8.3.00.0040.00419.13
8.2.180.0120.00318.04
8.2.170.0120.00322.96
8.2.160.0160.00620.52
8.2.150.0080.00024.18
8.2.140.0020.00524.66
8.2.130.0000.00826.16
8.2.120.0080.00021.16
8.2.110.0060.00320.39
8.2.100.0060.00618.22
8.2.90.0080.00019.22
8.2.80.0040.00417.97
8.2.70.0000.00817.59
8.2.60.0040.00418.05
8.2.50.0070.00418.07
8.2.40.0000.00817.97
8.2.30.0000.00818.05
8.2.20.0030.00517.67
8.2.10.0040.00418.11
8.2.00.0040.00417.77
8.1.280.0150.00325.92
8.1.270.0080.00023.72
8.1.260.0040.00426.35
8.1.250.0000.00728.09
8.1.240.0090.00022.25
8.1.230.0050.00518.95
8.1.220.0080.00017.79
8.1.210.0030.00518.77
8.1.200.0100.00017.47
8.1.190.0000.00817.35
8.1.180.0040.00418.10
8.1.170.0000.01018.71
8.1.160.0040.00422.13
8.1.150.0000.00818.89
8.1.140.0080.00017.49
8.1.130.0070.00017.88
8.1.120.0080.00017.56
8.1.110.0050.00317.54
8.1.100.0070.00017.55
8.1.90.0000.00817.57
8.1.80.0040.00417.42
8.1.70.0000.00717.45
8.1.60.0040.00417.65
8.1.50.0000.00817.46
8.1.40.0030.00617.51
8.1.30.0050.00517.66
8.1.20.0080.00017.71
8.1.10.0000.00817.50
8.1.00.0000.00917.41
8.0.300.0090.00018.77
8.0.290.0060.00316.75
8.0.280.0070.00018.45
8.0.270.0000.00817.14
8.0.260.0030.00317.31
8.0.250.0000.00716.91
8.0.240.0080.00017.00
8.0.230.0030.00316.91
8.0.220.0050.00316.87
8.0.210.0000.00716.94
8.0.200.0070.00016.89
8.0.190.0050.00316.91
8.0.180.0050.00316.96
8.0.170.0040.00417.00
8.0.160.0040.00416.91
8.0.150.0030.00616.77
8.0.140.0040.00416.89
8.0.130.0060.00313.45
8.0.120.0040.00416.91
8.0.110.0040.00416.84
8.0.100.0070.00016.83
8.0.90.0040.00416.81
8.0.80.0080.00816.88
8.0.70.0030.00616.82
8.0.60.0000.00717.02
8.0.50.0040.00416.76
8.0.30.0110.01317.27
8.0.20.0110.01017.40
8.0.10.0000.00917.02
8.0.00.0090.01116.80
7.4.330.0050.00015.02
7.4.320.0030.00316.48
7.4.300.0060.00016.55
7.4.290.0000.00716.64
7.4.280.0050.00316.65
7.4.270.0030.00716.43
7.4.260.0000.00916.52
7.4.250.0030.00616.58
7.4.240.0030.00316.56
7.4.230.0000.00716.66
7.4.220.0100.00716.61
7.4.210.0100.00516.43
7.4.200.0000.00716.73
7.4.160.0040.01216.46
7.4.150.0120.00617.40
7.4.140.0170.00517.86
7.4.130.0100.00816.52
7.4.120.0130.00816.62
7.4.110.0170.00016.38
7.4.100.0120.00616.36
7.4.90.0120.00616.44
7.4.80.0070.01019.39
7.4.70.0160.00416.63
7.4.60.0100.00716.36
7.4.50.0000.00616.21
7.4.40.0100.00716.60
7.4.30.0120.00316.60
7.4.00.0000.01515.02
7.3.330.0030.00313.17
7.3.320.0020.00213.32
7.3.310.0050.00216.20
7.3.300.0000.00716.33
7.3.290.0100.00316.35
7.3.280.0090.00716.29
7.3.270.0130.01017.40
7.3.260.0120.00616.25
7.3.250.0040.01216.47
7.3.240.0060.01016.43
7.3.230.0060.01616.40
7.3.210.0260.00916.39
7.3.200.0120.01219.39
7.3.190.0060.01616.41
7.3.180.0090.00616.29
7.3.170.0130.00416.58
7.3.160.0070.01016.33
7.3.120.0110.00814.70
7.3.110.0120.00615.10
7.3.100.0090.00614.80
7.3.90.0060.01215.08
7.3.80.0070.01014.80
7.3.70.0040.00715.01
7.3.60.0030.01314.96
7.3.50.0000.01914.79
7.3.40.0110.00414.84
7.3.30.0030.01014.57
7.3.20.0000.01216.50
7.3.10.0040.01116.38
7.3.00.0090.00616.80
7.2.330.0160.00716.62
7.2.320.0090.00916.75
7.2.310.0130.00616.76
7.2.300.0130.00716.73
7.2.290.0030.01316.50
7.2.240.0130.00614.92
7.2.230.0100.00314.99
7.2.220.0100.01014.97
7.2.210.0060.00914.89
7.2.200.0030.00714.84
7.2.190.0090.00615.06
7.2.180.0030.00915.15
7.2.170.0070.00714.83
7.2.160.0070.00714.93
7.2.150.0060.01216.58
7.2.140.0100.00316.60
7.2.130.0000.01316.91
7.2.120.0030.01016.81
7.2.110.0040.00816.88
7.2.100.0050.00516.87
7.2.90.0060.00916.66
7.2.80.0090.00616.74
7.2.70.0120.00317.03
7.2.60.0100.00316.76
7.2.50.0130.00316.77
7.2.40.0070.00716.91
7.2.30.0030.01316.55
7.2.20.0030.01316.80
7.2.10.0060.00616.88
7.2.00.0040.00918.01
7.1.330.0110.00615.72
7.1.320.0090.00615.83
7.1.310.0120.00315.80
7.1.300.0060.00315.52
7.1.290.0040.00715.43
7.1.280.0040.00415.51
7.1.270.0060.00915.67
7.1.260.0030.01015.74
7.1.250.0040.01115.69
7.1.100.0000.01017.86
7.1.70.0000.00716.99
7.1.60.0000.02519.46
7.1.50.0130.00716.54
7.1.00.0070.07322.48
7.0.200.0760.00914.64
7.0.140.0030.07321.95
7.0.100.0030.07320.05
7.0.90.0370.08320.10
7.0.80.0400.08320.01
7.0.70.0430.08020.01
7.0.60.0400.08320.06
7.0.50.0430.08020.44
7.0.40.0070.08020.01
7.0.30.0170.07720.11
7.0.20.0070.08320.08
7.0.10.0030.08720.04
7.0.00.0100.07720.05
5.6.280.0030.07720.98
5.6.250.0100.08020.70
5.6.240.0070.05020.73
5.6.230.0100.07720.57
5.6.220.0030.08720.66
5.6.210.0000.09020.71
5.6.200.0070.08721.16
5.6.190.0170.04321.16
5.6.180.0070.08021.09
5.6.170.0100.08321.00
5.6.160.0030.09021.02
5.6.150.0070.08321.07
5.6.140.0130.07321.05
5.6.130.0200.08021.12
5.6.120.0070.08321.09
5.6.110.0330.05021.07
5.6.100.0130.08021.00
5.6.90.0100.08021.16
5.6.80.0000.08020.59
5.6.70.0100.06320.61
5.6.60.0070.07720.50
5.6.50.0000.08320.50
5.6.40.0030.05720.46
5.6.30.0030.08020.60
5.6.20.0070.08020.42
5.6.10.0100.05020.37
5.6.00.0030.08720.43
5.5.380.0030.08320.42
5.5.370.0070.08020.59
5.5.360.0130.08020.42
5.5.350.0170.06720.50
5.5.340.0070.08320.69
5.5.330.0130.07320.95
5.5.320.0170.04020.71
5.5.310.0070.07720.99
5.5.300.0130.08020.89
5.5.290.0130.07320.98
5.5.280.0070.08320.95
5.5.270.0100.08720.82
5.5.260.0070.07020.87
5.5.250.0130.07720.70
5.5.240.0230.06320.33
5.5.230.0130.05720.34
5.5.220.0070.08020.22
5.5.210.0030.08020.08
5.5.200.0170.07320.23
5.5.190.0200.06720.30
5.5.180.0070.06720.32
5.5.160.0100.07720.29
5.5.150.0070.07720.21
5.5.140.0070.04720.34
5.5.130.0100.04320.15
5.5.120.0000.07320.30
5.5.110.0030.07720.21
5.5.100.0170.06720.18
5.5.90.0070.08020.25
5.5.80.0030.08320.18
5.5.70.0170.06020.23
5.5.60.0030.07320.12
5.5.50.0030.08320.00
5.5.40.0070.07320.13
5.5.30.0070.07720.11
5.5.20.0070.07720.07
5.5.10.0070.07320.03
5.5.00.0030.05020.04
5.4.450.0100.06319.45
5.4.440.0070.08019.22
5.4.430.0030.07719.37
5.4.420.0170.06719.45
5.4.410.0070.08319.33
5.4.400.0030.05319.21
5.4.390.0100.07319.04
5.4.380.0070.08019.19
5.4.370.0100.07319.05
5.4.360.0100.05019.23
5.4.350.0230.06019.04
5.4.340.0100.07319.23
5.4.320.0070.07319.06
5.4.310.0030.07019.24
5.4.300.0100.07319.11
5.4.290.0000.07719.21
5.4.280.0100.08019.21
5.4.270.0100.04319.16
5.4.260.0000.08319.21
5.4.250.0100.07319.25
5.4.240.0100.08019.16
5.4.230.0030.08319.04
5.4.220.0030.04018.95
5.4.210.0170.06319.05
5.4.200.0130.07318.95
5.4.190.0070.06319.18
5.4.180.0030.08019.04
5.4.170.0070.08019.04
5.4.160.0030.07319.21
5.4.150.0000.08019.07
5.4.140.0130.06016.26
5.4.130.0100.06316.34
5.4.120.0130.06316.32
5.4.110.0070.07016.53
5.4.100.0130.06716.56
5.4.90.0030.04316.48
5.4.80.0070.07016.36
5.4.70.0100.06716.38
5.4.60.0130.06716.33
5.4.50.0100.06716.53
5.4.40.0070.07016.43
5.4.30.0100.06716.50
5.4.20.0070.04716.54
5.4.10.0030.07716.48
5.4.00.0100.06715.86

preferences:
37.79 ms | 401 KiB | 5 Q