Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

CooL's avatar
Level 2

schedule and find gaps in booking

i need a help from with database-design and how to manage queries to store schedule of masters and find gaps to show to clients when master is available. I can use both mysql or postgres. I'm just stucked how to do this. Fiddle: http://sqlfiddle.com/#!9/071e21 Some description:

  1. Masters can have regular schedule like monday: 08:00-18:00, tuesday: 09:00-19:00 or odd/even days (for example master is working only on odd days of month) with the ability to set weekends (no matter odd/even day, but sunday is weekend no metter what).
  2. Masters have interval_between_clients_in_minutes param in settings. For example master have 60 minutes between clients, but someone booked visit from 08:00 to 09:30, that's mean that from 09:30 to 10:00 no one can book, because of this interval_between_clients (like a slots, 1 hour - 1 slot, if you book 1,5 hours - you book 2 slots).
  3. Master can add dayoff (like holidays, Christmas for example) or short working day.
  4. When client books visit to master he has total duration in minutes, for example 60 minutes, so if master have only 30 minutes available slot, that mean that this slot is not available and shoud not be included.

I have this tables for now:

masters: id - integer name - varchar interval_between_clients_in_minutes - integer

clients id - integer name - varchar

schedule id - integer master_id - integer day_of_week - integer open_time - time close_time - time schedule_type (1 - regular, 2 - odd/even)

schedule_exceptions id - integer master_id - integer exception_date - date exception_open_time - time exception_close_time - time exception_day_of_week - integer is_closed - boolean

visits master_id - integer client_id - integer visit_at - datetime visit_end - datetime duration_in_minutes - integer

  • How to find gaps to display it on calendar for example for month (start_date: 2018-03-01, end_date: 2018-04-01)

Expected result: Opening Hours of master for each day of above period EXCLUDE visits on each date.

Example: master is opened from 08:00-18:00, but have already visit from 09:00 to 10:00, that means that i need exclude 09:00-10:00 period and to display available gaps: 08:00-09:00, 10:00-18:00 for this particular day. If its weekend or holiday from schedule_exceptions - then no results.

0 likes
7 replies
burlresearch's avatar

I'm currently building a system that does something very similar. My approach in the end, and my advice to you, would be discretize your availability slots.

If you can move from timestamps for masters.opening_hours to slots (which can be of any duration, say on the 30 min, or 15 min interval) then your life becomes much easier.

Then you can start worrying about your availability and find logic and it will be much simpler. "Find all consecutive 3-slot openings" - rather than some very tedious work (in the continuous domain of 'timestamps').

Let us know what you think.

CooL's avatar
Level 2

Do you mean to generate slots for each day (Sunday, Monday, ...etc) slots from 08:00 to 20:00 for example by 15-30 minutes ? Can you give a short example, pls ?

burlresearch's avatar

I typed up a response, but it timed-out and was lost. Boo.

Ya so basically if you make 15min slots for 08h - 20h then that's 48 slots per weekday, consecutive from [1 .. 48]. If that's good enough for your purposes then when you are looking for slot availabilities, it's much easier to query for 6-consecutive open in [1 .. 48] than to do some sort of datestamp magic.

That's kind of my point. If you like this approach, then we can delve into actual code...

CooL's avatar
Level 2

it's good enough for regular schedule, what about odd/even schedule ? But it's still interesting to see actual code.

burlresearch's avatar

@CooL is this still relevant? I've been away - have you made progress on this beyond where we were?

CooL's avatar
Level 2

@burlresearch yes, relevant, i've already solved the problem, maybe not so efficient as it could be, but it works. But i want to refactor this if your approach will suite me.

Please or to participate in this conversation.