3v4l.org

run code in 300+ PHP versions simultaneously
<?php abstract class AbstractDateEntity { protected \DateTimeInterface $start; protected \DateTimeInterface $stop; protected WorkHours $workHours; public function __construct(string $start, string $stop, \DateTimeZone $tmz = null) { $tmz = $tmz ?? new DateTimeZone('Europe/Berlin'); $this->start = new DateTimeImmutable($start, $tmz); $this->stop = new DateTimeImmutable($stop, $tmz); $this->workHours = new WorkHours($this->getHours()); } public function getStart(): DateTimeInterface { return $this->start; } public function getStop(): DateTimeInterface { return $this->stop; } public function getDiff(): DateInterval { return $this->start->diff($this->stop); } public function getPeriod(DateInterval $interval = null): DatePeriod { return new DatePeriod($this->start, $interval ?? new DateInterval('PT1H'), $this->stop); } public function getWorkHours(): WorkHours { return $this->workHours; } public function getHours(): string { $diff = $this->getDiff(); $h = bcmul($diff->format('%a'), '24', 2); $mins = bcdiv($diff->i, '60', 2); return bcadd(bcadd($h, $diff->h, 2), $mins, 2); } } class Shift extends AbstractDateEntity { private array $absences = []; private array $absentPeriods = []; public function getAbsences(): array { return $this->absences; } public function addAbsence(Absence $absence): self { $this->absences[] = $absence; $this->workHours->addOff($absence->getWorkHours()->getTotal()); $this->absentPeriods = array_merge($this->absentPeriods, iterator_to_array($absence->getPeriod())); //here you can add logic for absences return $this; } public function isAbsent(DateTimeInterface $date): bool { return in_array($date, $this->absentPeriods); } public function getAbsentPeriods(): array { return $this->absentPeriods; } } class Absence extends AbstractDateEntity{} class WorkHours { private string $total = '0'; private string $off = '0'; private string $on = '0'; public function __construct(string $total = null, string $off = null) { if (null !== $total) { $this->total = $this->on = $total; } if (null !== $off) { $this->addOff($off); } } public function getTotal(): string { return $this->total; } public function addTotal(string $amount, int $p = 2): self { $this->total = bcadd($this->total, $amount, $p); $this->addOn($amount, $p); return $this; } public function getOff(): string { return $this->off; } public function addOff(string $amount, int $p = 2): self { $this->off = bcadd($this->off, $amount, $p); $this->subOn($amount, $p); return $this; } public function getOn(): string { return $this->on; } public function addOn(string $amount, int $p = 2): self { $this->on = bcadd($this->on, $amount, $p); return $this; } public function subOn(string $amount, int $p = 2): self { $this->on = bcsub($this->on, $amount, $p); return $this; } } class WorkSchedule { private array $shifts = []; private WorkHours $workHours; public function __construct() { $this->workHours = new WorkHours(); } public function getShifts(): array { return $this->shifts; } public function addShift(Shift $shift): self { $this->shifts[] = $shift; $this->workHours->addTotal($shift->getWorkHours()->getTotal()); $this->workHours->addOff($shift->getWorkHours()->getOff()); //here you can add logic to calculate the shift details return $this; } public function getWorkHours(): WorkHours { return $this->workHours; } } $tmz = new DateTimeZone('Europe/Berlin'); $workSchedule = (new WorkSchedule()) ->addShift( (new Shift("2022-04-02 10:00:00", "2022-04-02 20:00:00", $tmz)) ->addAbsence(new Absence("2022-04-02 15:00:00", "2022-04-02 17:00:00", $tmz), ) ) ->addShift( (new Shift("2022-04-03 7:00:00", "2022-04-03 17:00:00", $tmz)) ->addAbsence(new Absence("2022-04-03 16:00:00", "2022-04-03 17:00:00", $tmz)) ) ->addShift( (new Shift("2022-04-04 9:00:00", "2022-04-04 19:00:00", $tmz)) ->addAbsence(new Absence("2022-04-04 9:00:00", "2022-04-04 12:00:00", $tmz)) ); //report for totals $hrs = $workSchedule->getWorkHours(); vprintf("Total Hours Scheduled: %d\nTotal Hours Absent: %d\nTotal Hours Worked: %d\n\n", [ $hrs->getTotal(), $hrs->getOff(), $hrs->getOn(), ]); //reporting for each shift in the schedule foreach ($workSchedule->getShifts() as $shift) { vprintf("Shift %s - %s\n", [ $shift->getStart()->format('Y-m-d H:i:s'), $shift->getStop()->format('Y-m-d H:i:s') ]); $hrs = $shift->getWorkHours(); vprintf("Hours Scheduled: %d, Hours Absent: %d , Hours Worked: %d\n", [ $hrs->getTotal(), $hrs->getOff(), $hrs->getOn(), ]); //iterate over each workhour date $shiftPeriod = $shift->getPeriod(); foreach ($shiftPeriod as $shiftDate) { if ($shift->isAbsent($shiftDate)) { //was absent at this hour - do something? printf("Absent: %s\n", $shiftDate->format('H:i:s')); continue; } //was actively working at this hour - do something printf("Worked: %s\n", $shiftDate->format('H:i:s')); } echo PHP_EOL; }

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.5.30.0050.00320.18
8.5.20.0110.00620.32
8.5.10.0030.00520.23
8.5.00.0160.00720.49
8.4.180.0180.00520.06
8.4.170.0130.01220.57
8.4.160.0110.01123.68
8.4.150.0050.00417.02
8.4.140.0120.01018.13
8.4.130.0130.00818.91
8.4.120.0110.00922.25
8.4.110.0130.00722.59
8.4.100.0130.00818.19
8.4.90.0040.00718.75
8.4.80.0130.00918.04
8.4.70.0080.00917.98
8.4.60.0150.00718.78
8.4.50.0110.01119.66
8.4.40.0000.01019.79
8.4.30.0040.00418.78
8.4.20.0060.00318.45
8.4.10.0100.01020.54
8.3.300.0150.00620.91
8.3.290.0080.01320.79
8.3.280.0120.01018.63
8.3.270.0080.01217.12
8.3.260.0060.00316.80
8.3.250.0120.00619.11
8.3.240.0050.00516.80
8.3.230.0120.00816.99
8.3.220.0060.00317.37
8.3.210.0110.00917.10
8.3.200.0050.00417.06
8.3.190.0050.00517.53
8.3.180.0110.00718.47
8.3.170.0100.01020.61
8.3.160.0190.00018.71
8.3.150.0130.01019.14
8.3.140.0060.00317.09
8.3.130.0090.00018.53
8.3.120.0030.00619.06
8.3.110.0000.00916.87
8.3.100.0060.00317.01
8.3.90.0030.00626.77
8.3.80.0000.01017.13
8.3.70.0090.00618.56
8.3.60.0040.01218.80
8.3.50.0070.01420.38
8.3.40.0070.00719.15
8.3.30.0040.01119.38
8.3.20.0100.01024.18
8.3.10.0030.01524.66
8.3.00.0040.00426.16
8.2.300.0110.01022.32
8.2.290.0100.01120.32
8.2.280.0080.00622.57
8.2.270.0100.01017.24
8.2.260.0090.00918.84
8.2.250.0100.00616.83
8.2.240.0100.00618.57
8.2.230.0050.00520.94
8.2.220.0030.00624.06
8.2.210.0030.00626.77
8.2.200.0070.00318.42
8.2.190.0110.01116.63
8.2.180.0130.01025.92
8.2.170.0120.00319.20
8.2.160.0000.01522.96
8.2.150.0040.01525.66
8.2.140.0030.00624.66
8.2.130.0120.00626.16
8.2.120.0060.00326.16
8.2.110.0100.00020.84
8.2.100.0030.00918.11
8.2.90.0000.00819.51
8.2.80.0030.00517.97
8.2.70.0050.00318.00
8.2.60.0050.00517.92
8.2.50.0030.00618.10
8.2.40.0030.00618.47
8.2.30.0030.00618.50
8.2.20.0030.00620.68
8.2.10.0040.00418.26
8.2.00.0030.00519.41
8.1.340.0140.00617.86
8.1.330.0120.00721.86
8.1.320.0140.00616.16
8.1.310.0070.01318.76
8.1.300.0030.00517.89
8.1.290.0030.00630.84
8.1.280.0100.00625.92
8.1.270.0030.00623.99
8.1.260.0060.00326.35
8.1.250.0060.01228.09
8.1.240.0060.00323.86
8.1.230.0040.00817.82
8.1.220.0000.00917.90
8.1.210.0040.00418.77
8.1.200.0030.00617.60
8.1.190.0000.00817.73
8.1.180.0060.00318.10
8.1.170.0000.01018.79
8.1.160.0030.00519.04
8.1.150.0030.00718.84
8.1.140.0030.00517.67
8.1.130.0070.00018.98
8.1.120.0000.00717.63
8.1.110.0040.00417.76
8.1.100.0030.00617.63
8.1.90.0040.00417.66
8.1.80.0000.00817.77
8.1.70.0000.00717.78
8.1.60.0060.00317.86
8.1.50.0030.00517.70
8.1.40.0030.00517.67
8.1.30.0030.00817.75
8.1.20.0100.00217.83
8.1.10.0090.00317.66
8.1.00.0040.00817.57
8.0.300.0080.00018.77
8.0.290.0050.00317.00
8.0.280.0030.00318.65
8.0.270.0000.00817.19
8.0.260.0030.00318.54
8.0.250.0000.00717.20
8.0.240.0040.00717.26
8.0.230.0000.00717.30
8.0.220.0050.00317.23
8.0.210.0050.00317.21
8.0.200.0070.00017.27
8.0.190.0050.00317.24
8.0.180.0040.00417.29
8.0.170.0080.00417.21
8.0.160.0070.00417.09
8.0.150.0090.00317.17
8.0.140.0040.00417.17
8.0.130.0070.00417.06
8.0.120.0000.00917.10
8.0.110.0040.00417.20
8.0.100.0000.00717.11
8.0.90.0000.00817.08
8.0.80.0080.00017.13
8.0.70.0050.00317.23
8.0.60.0080.00417.07
8.0.50.0040.00717.20
8.0.30.0080.00317.28
8.0.20.0070.00417.25
8.0.10.0110.00017.36
7.4.330.0050.00015.55
7.4.320.0070.00016.83
7.4.300.0040.00416.72
7.4.290.0040.00416.78
7.4.280.0070.00416.65
7.4.270.0110.00316.69
7.4.260.0090.00616.70
7.4.250.0190.00316.80
7.4.240.0090.00616.67
7.4.230.0130.00316.85
7.4.220.0030.01116.57
7.4.210.0070.00716.82
7.4.200.0060.00616.71
7.4.190.0090.00216.87
7.4.180.0080.00316.75
7.4.160.0130.00016.62
7.4.150.0120.00016.73
7.4.140.0060.00616.59
7.4.130.0060.00616.66
7.4.120.0080.00316.71
7.4.110.0070.00316.68
7.4.100.0080.00516.54
7.4.90.0020.01216.71
7.4.80.0060.00616.67
7.4.70.0000.01216.48
7.4.60.0040.00416.63
7.4.50.0000.00816.42
7.4.40.0050.00316.55
7.4.30.0050.00516.55
7.4.20.0040.00816.61
7.4.10.0110.00016.61
7.4.00.0110.00016.68

preferences:
56.38 ms | 1498 KiB | 5 Q