3v4l.org

run code in 300+ PHP versions simultaneously
<?php trait TimeRestriction{ public function excludeTimes($times, $restrictions) { $timeList = $this->mergeTimes($times, $restrictions); var_dump($timeList); $validTimes = []; $result = []; foreach ($timeList as $key => $time) { $restrictionInclusive = false; if ($key == count($timeList) - 1) { $restrictionInclusive = true; } if ($this->isTimeWithinRanges($time, $times) && !$this->isTimeWithinRanges($time, $restrictions, $restrictionInclusive)) { $validTimes[] = $time; } } $validItemsCount = count($validTimes); for ($i = 0; $i < $validItemsCount; $i += 2) { $result[] = [ 'start_time' => $validTimes[$i], 'end_time' => $validTimes[$i + 1] ]; } return $result; } public function isTimeWithinRanges($time, $ranges, $inclusive = true) { foreach ($ranges as $range) { if ($this->isTimeWithinRange($time, $range, $inclusive)) { return true; } } return false; } public function isTimeWithinRange($time, $range, $inclusive = true) { if ($inclusive) { return $time->gte($range['start_time']) && $time->lte($range['end_time']); } else { return $time->gt($range['start_time']) && $time->lt($range['end_time']); } } public function mergeTimes() { $allTimes = collect([]); $params = func_get_args(); foreach ($params as $param) { foreach ($param as $timeRange) { $allTimes->push($timeRange['start_time']); $allTimes->push($timeRange['end_time']); } } var_dump($allTimes->toArray()); return $allTimes->sortBy(function (Carbon $value, $key) { return $value->getTimestamp(); })->values()->unique(function (Carbon $value) { return $value->getTimestamp(); })->all(); } }
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.6 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7

preferences:
158.79 ms | 404 KiB | 174 Q