Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Time slot for booking

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.

like image 815
Divyanshu Kumar Avatar asked Mar 09 '23 21:03

Divyanshu Kumar


1 Answers

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
        )
)
like image 78
Sanjay Bharti Avatar answered Mar 31 '23 01:03

Sanjay Bharti