1

i am developing a booking system for clients. i'm stuck at one place. The timeslots for the given date is generated dynamically by giving, start time, end time, duration and break time.

    $start = new DateTime($stTime);
    $end = new DateTime($enTime);
    $interval = new DateInterval("PT" . $duration. "M");
    $breakInterval = new DateInterval("PT" . $break. "M");
    for ($intStart = $start; 
         $intStart < $end; 
         $intStart->add($interval)->add($breakInterval)) {

           $endPeriod = clone $intStart;
           $endPeriod->add($interval);
           if ($endPeriod > $end) {
             $endPeriod=$end;
           }
           $starttime =  $intStart->format('h:iA');
           $endtime = $endPeriod->format('h:iA');
           $timeslot = $starttime.' - '.$endtime;
       }

Now its working successfully generating timeslots like this where duration and breaktime both are 15 mins: 09:00AM - 09:15AM
09:30AM - 09:45AM
10:00AM - 10:15AM
10:30AM - 10:45AM
11:00AM - 11:15AM
11:30AM - 11:45AM
12:00PM - 12:15PM When some one made a booking the date, starttime and endtime will get saved in database. and i can check that there is already a booking by using this query:

select * from bookings where starttime='$start' AND endtime='$end' AND bookingdate='$date'

Now the problem is the duration is dynamic. When i change duration to 30 mins. The new timeslots will be generated like this 09:00AM - 09:30AM
09:45AM - 10:15AM
10:30AM - 11:00AM
11:15AM - 11:45AM
12:00PM - 12:30PM

Now if want to check if the slot is already booked, the time slots are different and start and end times are different now from the ones saved in database. How can i tackle this situation? I want Old bookings with 15 mins duration to display Booked even with new Duration of 30 mins.

UPDATE Desired Result. when we have a booking of 10:00am to 10:30am before with 30 mins duration Desired results for 15 mins duration. 09:00AM - 09:15AM
09:30AM - 09:45AM
10:00AM - 10:30AM -- BOOKED 10:30AM - 10:45AM
11:00AM - 11:15AM
11:30AM - 11:45AM
12:00PM - 12:15PM

media net
  • 29
  • 3
  • Not sure if I understood correctly, but using `starttime>='$start' AND endtime<='$end'` wouldn't work? – Felippe Duarte Dec 17 '18 at 18:46
  • query the database to display all of 15 minutes and 30 minutes durations –  Dec 17 '18 at 18:48
  • im checking database with the dynamically generated start and end time. So if a booking already made with 30 mins duration 9am to 9:30am. so when we change duration to 15 mins the the generated timeslot will be 9am and 9:15am. How can i query and check if booking is already there? $starttime = $intStart->format('h:iA'); $endtime = $endPeriod->format('h:iA'); – media net Dec 17 '18 at 18:52
  • 2
    you need to check for _overlaps_ e.g. https://stackoverflow.com/questions/117962/what-is-a-simple-and-efficient-way-to-find-rows-with-time-interval-overlaps-in-s – ADyson Dec 17 '18 at 18:56
  • the booking is done with the item , lets say a room. now i have booked room no; 1 for 15 min, but somebody tries to book the same room, after lets say 5mins after am done. Now why dont you query using a room no. instead of the time stated - just a suggestion –  Dec 17 '18 at 19:05
  • because im displaying the timeslots available. i need to show 9:00AM - 09:30AM booked on html page. but when if i generate 15 min interval timeslot, the timeslot will display 09:00AM - 09:15AM, 09:15AM - 09:30AM. – media net Dec 17 '18 at 19:10
  • The bestway is to save you time interval 9:00AM to 09:30AM and then save 30mins in your database. then display room no. 1 , booked, call you start time and end time, plus the 30 min. But you need to create a cron to update your rows lets say if my time for room no. 1 expires to make it available again (use ajax). –  Dec 17 '18 at 19:16
  • I think some sample data and a desired result might be helpful – Strawberry Dec 17 '18 at 19:41
  • i've updated the question and added desired result at the end. thanks. – media net Dec 17 '18 at 19:49

0 Answers0