Can anyone suggest how to check a time period is overlapping another time period in the same day.
For example, I want to check the following conditions
1.) 7:00AM to 10:30AM is overlapping 10:00AM to 11:30AM
2.) 7:00AM to 10:30AM is overlapping 8:00AM to 9:00AM
3.) 7:00AM to 10:30AM is overlapping 5:00AM to 8:00AM
I have tried to write some IF conditions, but it is not working.
It’s a set of boolean conditions, there’s no better way to do it.
Draw it out on paper. Create a picture for every way the two ranges can overlap. Then write a boolean expression for each picture, and OR them all together into one big expression.
The simplest test I can think of for overlapping periods asuming the start date is less than the end date for each (as would be the case if the periods are correctly defined) would be:
period1_start < period2_end and period2_start < period1_end
<?php
$startTime = strtotime("7:00");
$endTime = strtotime("10:30");
$chkStartTime = strtotime("10:00");
$chkEndTime = strtotime("12:10");
if($chkStartTime > $startTime && $chkEndTime < $endTime)
{ #-> Check time is in between start and end time
echo "1 Time is in between start and end time";
}elseif(($chkStartTime > $startTime && $chkStartTime < $endTime) || ($chkEndTime > $startTime && $chkEndTime < $endTime))
{ #-> Check start or end time is in between start and end time
echo "2 ChK start or end Time is in between start and end time";
}elseif($chkStartTime==$startTime || $chkEndTime==$endTime)
{ #-> Check start or end time is at the border of start and end time
echo "3 ChK start or end Time is at the border of start and end time";
}elseif($startTime > $chkStartTime && $endTime < $chkEndTime)
{ #-> start and end time is in between the check start and end time.
echo "4 start and end Time is overlapping chk start and end time";
}
?>