<?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.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
- 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 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
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:
189.37 ms | 401 KiB | 184 Q