When building booking systems, appointment schedulers, or time-based applications, you often need to divide a given time range into smaller, manageable slots. In PHP, this can be efficiently handled using DateTime and loops to generate time intervals between a specified start and end time.
In this post, we’ll explore how to split time slots dynamically in PHP, ensuring flexibility for various use cases like reservations, meeting schedules, or event planning.
Let’s dive in!
Creating time between start time and end time in interval of 30 Minutes in PHP
$starttime = '9:00'; // your start time
$endtime = '21:00'; // End time
$duration = '30'; // split by 30 mins
$array_of_time = array ();
$start_time = strtotime ($starttime); //change to strtotime
$end_time = strtotime ($endtime); //change to strtotime
$add_mins = $duration * 60;
while ($start_time <= $end_time) // loop between time
{
$array_of_time[] = date ("h:i", $start_time);
$start_time += $add_mins; // to check endtie=me
}
Test it now
echo '<pre>';
print_r($array_of_time);
echo '</pre>';