<?php
$hours = ["08:00:00", "18:00:00"];
$bookings = [["10:00:00", "15:00:00"],
["08:05:00", "09:00:00"],
["16:00:00", "17:00:00"]
];
$start = $hours[0];
$end = $hours[1];
// sort the bookings
$bstart = array_column($bookings, 0);
array_multisort($bstart, $bookings);
// get the free times
$time = $start;
$bindex = 0;
$free = array();
while ($time < $end && $bindex < count($bookings)) {
if ($time < $bookings[$bindex][0]) {
$free[] = [$time, $bookings[$bindex][0]];
}
$time = $bookings[$bindex][1];
$bindex++;
}
// end of day free?
if ($time < $end) {
$free[] = [$time, $end];
}
print_r($free);
- Output for 5.5.38, 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.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
- Array
(
[0] => Array
(
[0] => 08:00:00
[1] => 08:05:00
)
[1] => Array
(
[0] => 09:00:00
[1] => 10:00:00
)
[2] => Array
(
[0] => 15:00:00
[1] => 16:00:00
)
[3] => Array
(
[0] => 17:00:00
[1] => 18:00:00
)
)
- Output for 5.4.0
- Fatal error: Call to undefined function array_column() in /in/CB0S0 on line 14
Process exited with code 255. - Output for 5.3.29
- Parse error: syntax error, unexpected '[' in /in/CB0S0 on line 3
Process exited with code 255.
preferences:
153.37 ms | 408 KiB | 5 Q