In this tip I can share you a simple but very useful piece of code. With help of this code, you can generate a time slot between any given number of the interval in a minute. For example, if you want to give a slot time for 15 minutes from 10 Am to 2 Pm you can use this code as shown below.

// getTimeSlot(IntervalInMinutes, $startTime, $endTime)
function getTimeSlot($interval, $start, $end)
{
    $start = new DateTime($start);
    $end = new DateTime($end);
    $start_time = $start->format('H:i'); // Get time Format in Hour and minutes
    $end_time = $end->format('H:i');
    $i=0;
    while(strtotime($start_time) <= strtotime($end_time)){
        $start = $start_time;
        $end = date('H:i',strtotime('+'.$interval.' minutes',strtotime($start_time)));
        $start_time = date('H:i',strtotime('+'.$interval.' minutes',strtotime($start_time)));
        $i++;
        if(strtotime($start_time) <= strtotime($end_time)){
            $time[$i]['start'] = $start;
            $time[$i]['end'] = $end;
        }
    }
    return $time;
}
$slot = getTimeSlot(15, '10:00', '13:00');

echo "<pre>";
print_r($slot);

Hope this code helps you 🙂

Thanks

Posted in: PHP