Partion 20 min slot of time in php from start and end time Partion 20 min slot of time in php from start and end time codeigniter codeigniter

Partion 20 min slot of time in php from start and end time


This will do what you need. But specific format should be kept for array elements, for easy calculations.

<?php$start_time = '2015-10-21 09:00:00';  //start time as string$end_time = '2015-10-21 19:45:00';  //end time as string$booked = array('12:20-12:40','13:00-13:20');    //booked slots as arrays$start = DateTime::createFromFormat('Y-m-d H:i:s',$start_time);  //create date time objects$end = DateTime::createFromFormat('Y-m-d H:i:s',$end_time);  //create date time objects$count = 0;  //number of slots$out = array();   //array of slots for($i = $start; $i<$end;)  //for loop {$avoid = false;   //booked slot?$time1 = $i->format('H:i');   //take hour and minute$i->modify("+20 minutes");      //add 20 minutes$time2 = $i->format('H:i');     //take hour and minute$slot = $time1."-".$time2;      //create a format 12:40-13:00 etc    for($k=0;$k<sizeof($booked);$k++)  //if booked hour    {    if($booked[$k] == $slot)  //check    $avoid = true;   //yes. booked    }if(!$avoid && $i<$end)  //if not booked and less than end time{$count++;           //add count$slots = ['start'=>$time1, 'stop'=>$time2];         //add countarray_push($out,$slots); //add slot to array}}var_dump($out);   //array outecho $count ." of slots available";

If you use booked datetime as array, use below code.

<?php$start_time = '2015-10-21 09:00:00';  //start time as string$end_time = '2015-10-21 19:45:00';  //end time as string$booked = ['2015-10-21 12:20:00','2015-10-21 12:40:00', '2015-10-21 13:00:00','2015-10-21 13:20:00'];     //booked slots as arrays$start = DateTime::createFromFormat('Y-m-d H:i:s',$start_time);  //create date time objects$end = DateTime::createFromFormat('Y-m-d H:i:s',$end_time);  //create date time objects$time1 = $start;$count = 0;  //number of slots$out = array();   //array of slots for($i = $start; $i<$end;)  //for loop {$avoid = false; $t1 = date_timestamp_get($i);$t2 = $t1+(20*60);    for($k=0;$k<sizeof($booked);$k+=2)  //if booked hour    {    $st = DateTime::createFromFormat('Y-m-d H:i:s',$booked[$k]);    $en = DateTime::createFromFormat('Y-m-d H:i:s',$booked[$k+1]);    if( $t1 >= date_timestamp_get($st) && $t2 <= date_timestamp_get($en)  )    $avoid = true;   //yes. booked    }$slots =[ $i->format('H:i'),$i->modify("+20 minutes")->format('H:i')];if(!$avoid && $i<$end)  //if not booked and less than end time{$count++;  array_push($out,$slots);  //add slot to array}}var_dump($out);   //array outecho $count ." of slots available";


You have to loop through slops up to the last sloop (which is less or equal to end time) at the same time we incease start time with 20 minutes each time and add it to the data array.

You can see more on PHP documentation page about functions I used: strtotime, date

<?php    $start_time = strtotime('2015-10-21 09:00:00');    $end_time = strtotime('2015-10-21 19:45:00');    $slot = strtotime(date('Y-m-d H:i:s',$start_time) . ' +20 minutes');    $data = [];    for ($i=0; $slot <= $end_time; $i++) {         $data[$i] = [             'start' => date('Y-m-d H:i:s', $start_time),            'end' => date('Y-m-d H:i:s', $slot),        ];        $start_time = $slot;        $slot = strtotime(date('Y-m-d H:i:s',$start_time) . ' +20 minutes');    }    print_r($data);?>