3v4l.org

run code in 300+ PHP versions simultaneously
<?php if (!function_exists('easter_date')) { /** * @see https://github.com/steinger/easter-date/blob/master/easter.php * @return int unix timestamp of the day of easter */ function easter_date(int $year): int { $J = date('Y', mktime(0, 0, 0, 1, 1, $year)); $K = floor( $J/ 100 ); $M = 15 + floor(( 3*$K+3 ) / 4 ) - floor(( 8*$K+13 ) / 25 ); $S = 2 - floor(( 3*$K+3 ) / 4 ); $A = $J%19; $D = ( 19 * $A + $M) % 30; $R = floor( $D / 29 ) + ( floor( $D / 28 ) - floor( $D / 29 )) * floor( $A / 11 ); $OG = 21 + $D - $R; $SZ = 7 - ( ($J + floor( $J / 4 ) + $S ) % 7 ); $OE = 7 - ( ($OG - $SZ) % 7 ); $OS = $OG + $OE; return mktime(0,0,0,3,$OS,$J); } } //observed closures $closures = [ '2017' => [ '2017-08-28', '2017-08-29', '2017-08-30', '2017-08-31', '2017-09-01' ] ]; file_put_contents(Holiday::CLOSURES_FILE, '<?php return ' . var_export($closures, true) . ';'); class Holiday { public const CLOSURES_FILE = '/tmp/closures.php'; private static $closuresLoaded = false; /** * date format = N (1 = Monday, ...) * @var array */ private static $workingDays = [1, 2, 3, 4, 5]; private static $observableHolidays = []; /** * Is submitted date a holiday? * * @param DateTime $date * @return bool */ public static function isHoliday(\DateTimeInterface $date): bool { $holidays = self::getHolidays($date); return \array_key_exists($date->format('Y-m-d'), \array_flip($holidays[$date->format('Y')])); } public static function isWorkDay(\DateTimeInterface $date): bool { if (!\in_array($date->format('N'), self::$workingDays)) { return false; } if (self::isHoliday($date)) { return false; } return true; } /** * Count number of weekdays within a given date span, excluding holidays * * @param \DateTimeInterface $from * @param \DateTimeInterface $to * @return int * @throws Exception */ public static function countWeekDays(\DateTimeInterface $from, \DateTimeInterface $to = null) { if (is_null($to)) { return null; } // from stackoverflow: // http://stackoverflow.com/questions/336127/calculate-business-days#19221403 $from = clone $from; $from->setTime(0, 0, 0); $to = clone $to; $to->setTime(0, 0, 0); $interval = new DateInterval('P1D'); $to->add($interval); $period = new DatePeriod($from, $interval, $to); $days = 0; /** @var DateTime $date */ foreach ($period as $date) { if (self::isWorkDay($date)) { $days++; } } return $days; } /** * Return count of weekdays in given month * * @param int $month * @param int $year * @return int * @throws Exception */ public static function countWeekDaysInMonthToDate(int $month, int $year): int { $today = new \DateTimeImmutable(); $d1 = $today->setDate($year, $month, 1); $d2 = $d1->modify('last day of this month'); return self::countWeekDays($d1, min($d2, $today)); } /** * @see https://www.php.net/manual/en/function.easter-date.php * @param \DateTimeInterface $date * @return \DateTimeImmutable */ private static function getEaster(\DateTimeInterface $date): \DateTimeImmutable { return (new \DateTimeImmutable())->setTimestamp(\easter_date($date->format('Y'))); } /** * Returns an array of strings representing holidays in format 'Y-m-d' * * @return array|string[][] */ public static function getHolidays(\DateTimeInterface $start): array { static $relativeHolidays = [ 'new years day' => 'first day of January', 'easter' => [__CLASS__, 'getEaster'], 'memorial day' => 'second Monday of May', 'independence day' => 'July 4th', 'labor day' => 'first Monday of September', 'thanksgiving' => 'fourth Thursday of November', 'black friday' => 'fourth Thursday of November + 1 day', 'christmas' => 'December 25th', 'new years eve' => 'last day of December', //... add others like Veterans Day, MLK, Columbus Day, etc ]; if (!$start instanceof \DateTimeImmutable) { //force using DateTimeImmutable $start = \DateTimeImmutable::createFromMutable($start); } //build the holidays to the specified year $start = $start->modify('first day of this year'); //always generate an entire years worth of holidays $period = new \DatePeriod($start, new \DateInterval('P1Y'), 0); foreach ($period as $date) { $year = $date->format('Y'); if (array_key_exists($year, self::$observableHolidays)) { continue; } self::$observableHolidays[$year] = []; foreach ($relativeHolidays as $relativeHoliday) { if (\is_callable($relativeHoliday)) { $holidayDate = $relativeHoliday($date); } elseif (0 === \strpos($relativeHoliday, 'P')) { $holidayDate = $date->add(new \DateInterval($relativeHoliday)); } else { $holidayDate = $date->modify($relativeHoliday); } self::$observableHolidays[$year][] = $holidayDate->format('Y-m-d'); } } //optionally do not use include file //return self::$observableHolidays; $holidays = self::$observableHolidays; if (\is_file(self::CLOSURES_FILE)) { foreach (include self::CLOSURES_FILE as $year => $dates) { if (!\array_key_exists($year, $holidays)) { $holidays[$year] = []; } $holidays[$year] = \array_merge($holidays[$year], $dates); } } return $holidays; } } $date = new \DateTime('2017-08-28'); $holidays = Holiday::getHolidays($date); var_export($holidays); echo \PHP_EOL; var_dump(Holiday::isHoliday($date));

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.4.130.0080.00318.05
8.4.120.0060.00420.66
8.4.110.0090.00822.65
8.4.100.0100.01018.38
8.4.90.0090.00922.22
8.4.80.0100.00819.91
8.4.70.0120.00818.32
8.4.60.0060.01118.99
8.4.50.0040.00520.16
8.4.40.0110.01118.18
8.4.30.0070.01020.12
8.4.20.0160.00320.46
8.4.10.0100.00719.88
8.3.260.0060.00416.82
8.3.250.0090.01018.82
8.3.240.0100.00916.74
8.3.230.0120.00916.72
8.3.220.0090.01017.45
8.3.210.0120.00716.90
8.3.200.0040.00616.61
8.3.190.0040.00417.54
8.3.180.0100.00917.38
8.3.170.0070.00417.04
8.3.160.0070.01316.79
8.3.150.0120.00620.85
8.3.140.0050.00316.88
8.3.130.0030.00618.49
8.3.120.0060.00320.78
8.3.110.0070.01320.94
8.3.100.0070.00324.06
8.3.90.0110.00726.77
8.3.80.0060.00318.43
8.3.70.0090.00616.71
8.3.60.0150.00618.81
8.3.50.0110.00716.71
8.3.40.0120.00618.92
8.3.30.0040.01518.96
8.3.20.0090.00020.96
8.3.10.0030.00620.55
8.3.00.0040.00423.48
8.2.290.0100.00920.39
8.2.280.0050.00718.68
8.2.270.0080.01117.40
8.2.260.0050.00320.36
8.2.250.0070.00718.56
8.2.240.0030.00619.22
8.2.230.0000.00822.58
8.2.220.0130.00337.54
8.2.210.0130.00326.77
8.2.200.0030.00616.88
8.2.190.0120.00317.13
8.2.180.0000.01625.92
8.2.170.0120.00322.96
8.2.160.0070.00720.64
8.2.150.0030.00624.18
8.2.140.0090.00024.66
8.2.130.0050.00226.16
8.2.120.0030.00626.16
8.2.110.0030.00622.32
8.2.100.0090.00318.28
8.2.90.0000.00818.13
8.2.80.0080.00018.00
8.2.70.0120.00818.62
8.2.60.0050.01418.62
8.2.50.0130.00618.62
8.2.40.0080.00818.62
8.2.30.0150.00218.62
8.2.20.0130.00418.62
8.2.10.0060.01318.62
8.2.00.0180.00018.62
8.1.330.0080.01122.25
8.1.320.0110.00916.45
8.1.310.0060.00318.75
8.1.300.0040.00418.48
8.1.290.0090.00030.84
8.1.280.0130.01025.92
8.1.270.0060.00321.02
8.1.260.0080.00026.35
8.1.250.0040.00428.09
8.1.240.0000.00924.02
8.1.230.0100.00019.25
8.1.220.0000.00818.00
8.1.210.0050.00818.77
8.1.200.0100.00718.62
8.1.190.0130.00318.62
8.1.180.0150.00218.62
8.1.170.0160.00018.62
8.1.160.0170.00018.62
8.1.150.0130.00318.62
8.1.140.0150.00318.62
8.1.130.0120.00618.62
8.1.120.0170.00018.62
8.1.110.0160.00018.62
8.1.100.0090.00918.62
8.1.90.0140.00518.62
8.1.80.0120.00618.62
8.1.70.0110.00518.62
8.1.60.0150.00318.62
8.1.50.0110.00718.62
8.1.40.0140.00518.62
8.1.30.0160.00218.62
8.1.20.0190.00018.62
8.1.10.0110.00818.62
8.1.00.0110.00818.62
8.0.300.0030.00620.02
8.0.290.0040.01218.62
8.0.280.0120.00618.62
8.0.270.0170.00018.62
8.0.260.0160.00018.62
8.0.250.0080.00818.62
8.0.240.0130.00418.62
8.0.230.0190.00018.62
8.0.220.0110.00618.62
8.0.210.0110.00618.62
8.0.200.0170.00018.62
8.0.190.0170.00018.62
8.0.180.0120.00718.62
8.0.170.0090.00918.62
8.0.160.0120.00418.62
8.0.150.0040.01318.62
8.0.140.0150.00218.62
8.0.130.0130.00418.62
8.0.120.0090.00918.62
8.0.110.0150.00018.62
8.0.100.0060.01218.62
8.0.90.0180.00018.62
8.0.80.0100.00718.62
8.0.70.0110.00618.62
8.0.60.0110.00618.62
8.0.50.0110.00718.62
8.0.30.0140.00218.62
8.0.20.0170.00018.62
8.0.10.0140.00518.62
8.0.00.0040.01318.62
7.4.330.0090.00418.62
7.4.320.0150.00418.62
7.4.300.0130.00418.62
7.4.290.0080.00818.62
7.4.280.0100.00518.62
7.4.270.0120.00418.62
7.4.260.0100.00718.62
7.4.250.0110.00818.62
7.4.240.0140.00218.62
7.4.230.0130.00318.62
7.4.220.0050.01018.62
7.4.210.0050.01018.62
7.4.200.0160.00018.62
7.4.190.0160.00018.62
7.4.180.0090.00918.62
7.4.160.0110.00418.62
7.4.150.0050.01018.62
7.4.140.0090.00618.62
7.4.130.0090.00918.62
7.4.120.0080.00818.62
7.4.110.0100.00718.62
7.4.100.0080.00818.62
7.4.90.0110.00618.62
7.4.80.0110.00418.62
7.4.70.0060.01118.62
7.4.60.0170.00018.62
7.4.50.0090.00618.62
7.4.40.0130.00318.62
7.4.30.0040.01218.62
7.4.20.0120.00418.62
7.4.10.0130.00318.62
7.4.00.0040.01218.62
7.3.330.0090.00618.62
7.3.320.0080.00818.62
7.3.310.0130.00318.62
7.3.300.0110.00418.62
7.3.290.0080.00818.62
7.3.280.0130.00318.62
7.3.270.0140.00018.62
7.3.260.0090.00618.62
7.3.250.0090.00618.62
7.3.240.0150.00018.62
7.3.230.0130.00318.62
7.3.220.0080.00818.62
7.3.210.0110.00518.62
7.3.200.0060.00918.62
7.3.190.0160.00018.62
7.3.180.0160.00018.62
7.3.170.0090.00918.62
7.3.160.0160.00018.62
7.3.150.0120.00418.62
7.3.140.0100.00618.62
7.3.130.0040.01218.62
7.3.120.0150.00018.62
7.3.110.0150.00018.62
7.3.100.0110.00418.62
7.3.90.0090.00918.62
7.3.80.0150.00018.62
7.3.70.0150.00018.62
7.3.60.0140.00018.62
7.3.50.0090.00618.62
7.3.40.0120.00418.62
7.3.30.0090.00618.62
7.3.20.0170.00018.62
7.3.10.0130.00518.62
7.3.00.0130.00418.62
7.2.340.0100.00518.62
7.2.330.0070.01018.62
7.2.320.0120.00418.62
7.2.310.0110.00518.62
7.2.300.0050.01118.62
7.2.290.0140.00018.62
7.2.280.0090.00618.62
7.2.270.0070.00718.62
7.2.260.0090.00618.62
7.2.250.0090.00918.62
7.2.240.0130.00318.62
7.2.230.0130.00218.62
7.2.220.0150.00018.62
7.2.210.0150.00018.62
7.2.200.0040.01218.62
7.2.190.0060.00918.62
7.2.180.0080.00818.62
7.2.170.0120.00418.62
7.2.160.0070.00718.62
7.2.150.0170.00018.62
7.2.140.0100.00518.62
7.2.130.0170.00018.75
7.2.120.0150.00018.62
7.2.110.0080.00818.62
7.2.100.0130.00518.62
7.2.90.0090.01018.62
7.2.80.0110.00518.62
7.2.70.0060.01118.75
7.2.60.0080.00818.62
7.2.50.0140.00318.62
7.2.40.0110.00418.64
7.2.30.0170.00018.62
7.2.20.0180.00018.70
7.2.10.0060.01218.62
7.2.00.0120.00418.62
7.1.330.0000.01718.62
7.1.320.0050.01018.62
7.1.310.0160.00018.62
7.1.300.0110.00418.62
7.1.290.0120.00618.62
7.1.280.0080.00818.62
7.1.270.0050.01118.62
7.1.260.0070.00718.62
7.1.250.0140.00018.62
7.1.240.0090.00618.62
7.1.230.0150.00018.62
7.1.220.0100.00518.62
7.1.210.0000.01618.62
7.1.200.0150.00018.62
7.1.190.0090.00618.62
7.1.180.0110.00418.62
7.1.170.0130.00318.62
7.1.160.0120.00318.62
7.1.150.0140.00318.62
7.1.140.0090.00618.62
7.1.130.0170.00018.62
7.1.120.0100.00318.62
7.1.110.0150.00018.62
7.1.100.0140.00018.62
7.1.90.0130.00318.62
7.1.80.0100.00518.62
7.1.70.0100.00818.62
7.1.60.0140.00418.62
7.1.50.0060.01218.62
7.1.40.0150.00218.62
7.1.30.0140.00318.62
7.1.20.0120.00318.62
7.1.10.0120.00418.62
7.1.00.0100.00518.62

preferences:
36.1 ms | 403 KiB | 5 Q