How to check if a timerange falls under an array of free slots?

Haptundanub

New Member
I am after a method, that will check if a timerange, falls under some free slots.I have a method that returns free slots in unixtime format \[code\]array['from','to']\[/code\]. Visually something like:\[code\]07:00 - 07:4507:45 - 08:3014:30 - 15:1515:15 - 16:00\[/code\]I want to check if a given range falls under these slots. They can overlap slots, but should not span period ouf the free slot. So valid range is:\[code\]07:00 - 07:4507:00 - 08:3007:10 - 08:20 // also valid14:30 - 16:00 // also valid\[/code\]Invalid range is\[code\]06:00 - 16:4515:15 - 16:1007:00 - 16:00 //there are no ranges between 8:30 and 14:3011:00 - 12:00\[/code\]EDITWhat I've so far is to check is:\[code\]$startsinfreeslot=true;$endrange=0;foreach ($arr as $slot) {$from=$slot['from'];$to=$slot['to'];if ($from==$start_date && $to==$end_date) { // given date matches a free slot return true;}if ($start_date>=$from && $start_date<$to) { $startsinfreeslot=true; $endrange=$to; continue;}if ($startsinfreeslot) { // we need to check if range continues if ($endrange==$from) { // range stops in the current range if ($end_date<=$to) { return true; } else { $endrange=$to; continue; } } else { // range doesn't continued return false return false; }}}\[/code\]
 
Back
Top