I have a table called bookings, that have the following columns & data:
ID | Client | Start At          | End At
1    103      2020-12-17 14:15    2020-12-17 14:30
2    202      2020-12-17 14:35    2020-12-17 15:00
3    204      2020-12-17 16:00    2020-12-17 16:20
4    158      2020-12-17 17:00    2020-12-17 18:00
5    157      2020-12-19 10:00    2020-12-19 10:15
6    142      2020-12-21 10:00    2020-12-21 10:15
The creation of bookings have the following rules:
- Sundays are not available
 - Monday to friday availability from 10am to 7pm
 - Saturday availability from 10am to 8pm
 - Bookings duration is dynamic (can be 5 minutes, 30min, 1h, 2h, etc)
 
So far my code only checks if the certain date is occupied. Lets say I want to check if the date 2020-12-17 14:20 to 2020-12-17 14:30 is available.
SELECT * 
FROM bookings 
WHERE 
(
    (start_at <= '2020-12-17 14:20' AND end_at >= '2020-12-17 14:20') OR 
    (start_at <= '2020-12-17 14:30' AND end_at >= '2020-12-17 14:30') OR 
    (start_at >= '2020-12-17 14:20' AND end_at <= '2020-12-17 14:30')
)
This works great and would return that the date is not available (as you can see in the ID 1).
What I need:
- To suggest my users the next available date taking in consideration the rules
 - To suggest the closest next date after the selected date
 
So, lets have the following example:
- User selects date 
2020-12-17 14:40to2020-12-17 15:00[20 min] [Occupied] [Suggest:2020-12-17 15:00] - User selects date 
2020-12-17 18:00to2020-12-17 19:15[1h15 min] [Occupied] [Suggest:2020-12-18 10:00] - User selects date 
2020-12-21 10:10to2020-12-21 10:20[10 min] [Occupied] [Suggest:2020-12-21 10:15] - User selects date 
2020-12-21 12:00to2020-12-21 12:20[20 min] [Free] 
Can I achieve this with plain SQL or do I need to put some PHP logic behind it?