Hi Can someone help on how to validate if the Time is between the time for example if a person1 has a scheduled time 1PM to 4PM then the person2 must not have scheduled between those time how can i validate it? i can only validate if the person2 time is equal to person1 time.
1 Answers
To check if two time ranges overlap, this is the algorithm.
$startTime1 < $endTime2 && $endTime1 > $startTime2
If you don't want to just show other event reservations, you would need the person trying to "schedule" a time to enter in what they would like and you compare it to existing events. You would likely be better off from a UI standpoint to just ask the user how long they want and show them when the room would be available for that length of time (or close, e.g. if someone said they needed 2 hours and there is a block of 1:45 available sooner than a 2 hour block let them know about it to make a choice).
This covers all 4 possible overlaps, A inside B, B inside A and partial overlaps of start/end. Also, this will consider two events that butt up against each other (e.g. 9-10 & 10-11) as not overlapping. If you want to consider them overlapping, you can change the less/greater than (< and >
) to include equals like >= and <=
.

- 15,279
- 3
- 32
- 43