Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intersecting time slots

I am working on a booking system which works around the user selecting a time slot and I am stuck attempting to check for overlapping bookings.

I am attempting to create a function which returns all the available time slots. I have an array called bookings which are the bookings for that day, and I have an array called slots.

I have tried looping through the arrays and attempting this:

if (slot.start > booking.dateStart.clone().addMinutes(-30) && slot.end < booking.dateEnd.clone().addMinutes(30))

To check for any overlap, and if there is I splice the slot from the array. I need to leave a 30 minute gap between each booking so that is why I am adding minutes.

like image 671
user2117190 Avatar asked Feb 02 '26 05:02

user2117190


1 Answers

The formula to find overlapping time periods is

start1 <= end2 && end1 >= start2

If that is true, then the periods do overlap. If you need to leave a 30 minute gap, just add that to the comparison of end1 and start2.

like image 169
krillgar Avatar answered Feb 04 '26 18:02

krillgar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!