Split a Time Slot Between the Start and End Time Using the Time Interval

This is a user-defined PHP function that is used to split a time slot between the start and end time using the time interval.

The function takes a start time, end time and time interval as input and gives an array as the output.

Function:
function SplitTime($StartTime, $EndTime, $Duration="60"){
    $ReturnArray = array ();// Define output
    $StartTime    = strtotime ($StartTime); //Get Timestamp
    $EndTime      = strtotime ($EndTime); //Get Timestamp

    $AddMins  = $Duration * 60;

    while ($StartTime <= $EndTime) //Run loop
    {
        $ReturnArray[] = date ("G:i", $StartTime);
        $StartTime += $AddMins; //Endtime check
    }
    return $ReturnArray;
}

//Calling the function
$Data = SplitTime("2018-05-12 12:15", "2018-05-12 15:30", "60");
echo "<pre>";
print_r($Data);
echo "<pre>";
Output:
Array
(
    [0] => 12:15
    [1] => 13:15
    [2] => 14:15
    [3] => 15:15
)

Courses
Subscribe Updates via Email

Join 49,000+ W3schools lovers and get all the latest tutorials, programs, algorithms in your inbox.