I have an array of timestamps and duration and an array of occupied timestamps. I now need to check if these timestamps collide.
Basically $start[] cannot be within any of the $occupied[] timestamps
$start[0] = 1486987200; // 12:00
$duration[0] = 3600;
$start[1] = 1487008800; // 18:00
$duration[1] = 7200;
$occupied[0] = 1486989000; // 12:30
$ocDuration[0] = 3600;
$occupied[1] = 1487019600; // 21:00
$ocDuration[1] = 7200;
From the above $start[0] is not possible because $occupied[0] is within its range of 1 hour (3600 seconds), but $start[1] IS possible because it starts at 18:00, and ends 2 hours later.

Another situation could be when $occupied[0] overlaps both $start[]:

So the question is, how can i do such a check ?
If you will use $start and $duration as non-array variables, you can use this one below. Otherwise, just write a double for-loop.
$start[0] = 1486987200; // 12:00
$duration[0] = 3600;
$start[1] = 1487008800; // 18:00
$duration[1] = 7200;
$occupied[0] = 1486989000; // 12:30
$ocDuration[0] = 3600;
$occupied[1] = 1487019600; // 21:00
$ocDuration[1] = 7200;
$occupied[2] = 1486989000; // 12:30
$ocDuration[2] = 23400;
function checkOccupancy($start, $duration, $occupied, $ocDuration){
$ocLength = count($occupied);
for($i = 0; $i <= $ocLength; $i++){
$ocEnd = $occupied[$i] + $ocDuration[$i];
$end = $start + $duration;
if(($start > $occupied[$i] && $start < $ocEnd) || ($end > $occupied[$i] && $end < $ocEnd) ){
return "Not Possible";
}
}
return "Possible";
}
echo checkOccupancy($start[0], $duration[0], $occupied, $ocDuration);
echo checkOccupancy($start[1], $duration[1], $occupied, $ocDuration);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With