Split time slots in PHP based on given start and end time intervals.
function getTimeSlot($interval, $start_time, $end_time)
{
$start = new DateTime($start_time);
$end = new DateTime($end_time);
$startTime = $start->format('H:i');
$endTime = $end->format('H:i');
$i=0;
$time = [];
while(strtotime($startTime) <= strtotime($endTime)){
$start = $startTime;
$end = date('H:i',strtotime('+'.$interval.' minutes',strtotime($startTime)));
$startTime = date('H:i',strtotime('+'.$interval.' minutes',strtotime($startTime)));
$i++;
if(strtotime($startTime) <= strtotime($endTime)){
$time[$i]['slot_start_time'] = $start;
$time[$i]['slot_end_time'] = $end;
}
}
return $time;
}
function getTodaysTimeSlots($duration, $start, $end)
{
$time = array();
$start = new \DateTime($start);
$end = new \DateTime($end);
$start_time = $start->format('H:i');
$end_time = $end->format('H:i');
$currentTime = strtotime(Date('Y-m-d 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)));
$today = Date('Y-m-d');
$slotTime = strtotime($today.' '.$start);
if($slotTime > $currentTime){
if(strtotime($start_time) <= strtotime($end_time)){
$time[$i]['start'] = $start;
$time[$i]['end'] = $end;
}
$i++;
}
}
return $time;
}