3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Period { protected $start; protected $end; public function __construct($start, $end) { $this->start = $start; $this->end = $end; } public function getStart() { return $this->start; } public function getEnd() { return $this->end; } public function containsTime($time) { return $this->start <= $time && $this->end > $time; } public function containsPeriod(Period $period) { return $this->containsTime($period->getStart()) && $this->containsTime($period->getEnd()); } public function overlaps(Period $period) { return $this->containsTime($period->getStart()) || $this->containsTime($period->getEnd()) || $period->containsTime($this->start); } public function intersect(Period $other) { $ts = $this->start; $os = $other->start; $te = $this->end; $oe = $other->end; if ($this->containsTime($os)) { $start = $os; } else if ($other->containsTime($ts)) { $start = $ts; } else { return null; } if ($this->containsTime($oe)) { $end = $oe; } else if ($other->containsTime($te)) { $end = $te; } else { return null; } return new Period($start, $end); } } class Availability { protected $periods; public function __construct($periods) { $this->periods = $periods; } public function getPeriods(Period $range) { return array_filter($this->periods, function ($period) use ($range) { return $range->intersect($period); }); } } function st($s) { return strtotime($s); } function pd($s1, $s2) { return new Period(st($s1), st($s2)); } function av() { $p = func_get_args(); $p = array_map(function ($p) { return pd($p[0], $p[1]); }, $p); return new Availability($p); } function strPeriod(Period $period) { return date('H:i:s . jS M y', $period->getStart()) . ' -- ' . date('H:i:s . jS M y', $period->getStart()); } function strResults($periods) { return implode(PHP_EOL, array_map('strPeriod', $periods)); } $availabilities = [ av( // Fridays between noon and 6pm ['2018-10-05 12:00:00', '2018-10-05 18:00:00'], ['2018-10-12 12:00:00', '2018-10-12 18:00:00'], ['2018-10-19 12:00:00', '2018-10-19 18:00:00'], ['2018-10-26 12:00:00', '2018-10-26 18:00:00'] ), av( // 15th till 19th full days ['2018-10-15 00:00:00', '2018-10-16 00:00:00'], ['2018-10-16 00:00:00', '2018-10-17 00:00:00'], ['2018-10-17 00:00:00', '2018-10-18 00:00:00'], ['2018-10-18 00:00:00', '2018-10-19 00:00:00'], ['2018-10-19 00:00:00', '2018-10-20 00:00:00'] ), av( // Wed 10th, Fri 19th and Wed 24th between 8am and 2pm ['2018-10-10 08:00:00', '2018-10-10 14:00:00'], ['2018-10-19 08:00:00', '2018-10-19 14:00:00'], ['2018-10-24 08:00:00', '2018-10-24 14:00:00'] ), ]; // Get periods for the whole month of October $periods = [ pd('2018-10-01 00:00:00', '2018-11-01 00:00:00') ]; foreach ($availabilities as $i => $avail) { printf('Availability #%d' . PHP_EOL, $i); echo str_repeat('-', 80) . PHP_EOL; $tPeriods = []; foreach ($periods as $period) { printf(' > Getting available periods in %s'.PHP_EOL, strPeriod($period)); $aPeriods = $avail->getPeriods($period); printf(' > Got %d periods', count($aPeriods)); $tPeriods = array_merge($tPeriods, $aPeriods); } $periods = $tPeriods; } echo str_repeat('=', 80) . PHP_EOL; echo 'RESULTS:' . PHP_EOL; echo strResults($periods);
Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 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.18, 8.3.0 - 8.3.4, 8.3.6
Availability #0 -------------------------------------------------------------------------------- > Getting available periods in 00:00:00 . 1st Oct 18 -- 00:00:00 . 1st Oct 18 > Got 4 periodsAvailability #1 -------------------------------------------------------------------------------- > Getting available periods in 12:00:00 . 5th Oct 18 -- 12:00:00 . 5th Oct 18 > Got 0 periods > Getting available periods in 12:00:00 . 12th Oct 18 -- 12:00:00 . 12th Oct 18 > Got 0 periods > Getting available periods in 12:00:00 . 19th Oct 18 -- 12:00:00 . 19th Oct 18 > Got 1 periods > Getting available periods in 12:00:00 . 26th Oct 18 -- 12:00:00 . 26th Oct 18 > Got 0 periodsAvailability #2 -------------------------------------------------------------------------------- > Getting available periods in 00:00:00 . 19th Oct 18 -- 00:00:00 . 19th Oct 18 > Got 1 periods================================================================================ RESULTS: 08:00:00 . 19th Oct 18 -- 08:00:00 . 19th Oct 18
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Availability #0 -------------------------------------------------------------------------------- > Getting available periods in 00:00:00 . 1st Oct 18 -- 00:00:00 . 1st Oct 18 > Got 4 periodsAvailability #1 -------------------------------------------------------------------------------- > Getting available periods in 12:00:00 . 5th Oct 18 -- 12:00:00 . 5th Oct 18 > Got 0 periods > Getting available periods in 12:00:00 . 12th Oct 18 -- 12:00:00 . 12th Oct 18 > Got 0 periods > Getting available periods in 12:00:00 . 19th Oct 18 -- 12:00:00 . 19th Oct 18 > Got 1 periods > Getting available periods in 12:00:00 . 26th Oct 18 -- 12:00:00 . 26th Oct 18 > Got 0 periodsAvailability #2 -------------------------------------------------------------------------------- > Getting available periods in 00:00:00 . 19th Oct 18 -- 00:00:00 . 19th Oct 18 > Got 1 periods================================================================================ RESULTS: 08:00:00 . 19th Oct 18 -- 08:00:00 . 19th Oct 18

preferences:
205.76 ms | 404 KiB | 287 Q