3v4l.org

run code in 300+ PHP versions simultaneously
<?php function isWeekend($date) { if ($date instanceof DateTime) { $date = DateTimeImmutable::createFromMutable($date); } return $date->setTime(0,0,0) != $date->modify('this weekday'); } function isWeekendUnixTimestamp($dateValue) { return date('Yz', strtotime($dateValue)) != date('Yz', strtotime($dateValue . ' this weekday')); } echo 'DateTimeInterface Comparison:' . PHP_EOL; $sunday = new DateTimeImmutable('Sunday'); $periods = new DatePeriod($sunday, new DateInterval('P1D'), 6); foreach ($periods as $date) { echo $date->format('D') . ' is' . (isWeekend($date) ? '' : ' not') . ' a weekend' . PHP_EOL; } echo PHP_EOL . 'String Comparison:' . PHP_EOL; foreach ($periods as $date) { echo $date->format('D') . ' is' . (isWeekendUnixTimestamp($date->format('Y-m-d')) ? '' : ' not') . ' a weekend' . PHP_EOL; }
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.26, 8.1.0 - 8.1.13
DateTimeInterface Comparison: Sun is a weekend Mon is not a weekend Tue is not a weekend Wed is not a weekend Thu is not a weekend Fri is not a weekend Sat is a weekend String Comparison: Sun is a weekend Mon is not a weekend Tue is not a weekend Wed is not a weekend Thu is not a weekend Fri is not a weekend Sat is a weekend
Output for 4.3.0
Parse error: parse error, unexpected T_STRING in /in/TSAVi on line 5
Process exited with code 255.

preferences:
52.92 ms | 410 KiB | 5 Q