4

I want to create a time slot in which booking should be allowed from 10:00 AM till 07:00 PM.

Condition: each service time 60 min & 0 min break. like This,

10:00 AM - 11:00 AM
11:00 AM - 12:00 AM
12:00 AM - 13:00 AM
13:00 AM - 14:00 PM

Can you guys provide me the solutions for this problem? Thanks in advance.

  • Can you please add whatever you have tried so far? – Akshay Feb 14 '17 at 08:03
  • i haven't try anything just right since i am not getting clue to how to do it. i have find some solution in the stackover flow but i am unable to get in which format i should send the variable data. one of the solution is here http://stackoverflow.com/questions/20606770/php-create-timeslots-with-break-timing-using-dateperiod/42220274?noredirect=1#comment71600848_42220274 – Divyanshu Kumar Feb 14 '17 at 08:32

1 Answers1

7

Try this ...

function getServiceScheduleSlots($duration, $start,$end)
{
        $start = new DateTime($start);
        $end = new DateTime($end);
        $start_time = $start->format('H:i');
        $end_time = $end->format('H:i');
        $i=0;
        while(strtotime($start_time) <= strtotime($end_time)){
            $start = $start_time;
            $end = date('H:i',strtotime('+'.$duration.' minutes',strtotime($start_time)));
            $start_time = date('H:i',strtotime('+'.$duration.' minutes',strtotime($start_time)));
            $i++;
            if(strtotime($start_time) <= strtotime($end_time)){
                $time[$i]['start'] = $start;
                $time[$i]['end'] = $end;
            }
        }
        return $time;
  }
$slopt = getServiceScheduleSlots(60, '10:00AM', '07:00PM')

Result will be like this:

Array
(
    [1] => Array
        (
            [start] => 10:00
            [end] => 11:00
        )
    [2] => Array
        (
            [start] => 11:00
            [end] => 12:00
        )
    [3] => Array
        (
            [start] => 12:00
            [end] => 13:00
        )
    [4] => Array
        (
            [start] => 13:00
            [end] => 14:00
        )
    [5] => Array
        (
            [start] => 14:00
            [end] => 15:00
        )
    [6] => Array
        (
            [start] => 15:00
            [end] => 16:00
        )
    [7] => Array
        (
            [start] => 16:00
            [end] => 17:00
        )
    [8] => Array
        (
            [start] => 17:00
            [end] => 18:00
        )
    [9] => Array
        (
            [start] => 18:00
            [end] => 19:00
        )
)
Sanjay Bharti
  • 166
  • 3
  • 5