I developed a function to obtain equal time intervals within a user generated time-frame.
$start = strtotime("12:00"); //start at...
$end = strtotime("18:00"); //end at...
$timeframe = $end - $start; //time-frame
$intervals = 3; //number of intervals within time-frame
$interval_time = $timeframe/$intervals; //time of each interval
for($i = 0, $start;
$i < $intervals;
$i++, $start = strtotime("+$interval_time seconds", $start)) //increment time
{
$new_time = date('H:i:s a', $start);
echo "$new_time \n";
}
The code above outputs
12:00:00 pm 14:00:00 pm 16:00:00 pm
What code can I incorporate to divide each interval and obtain the middle? For example to get from the code above
13:00:00 pm 15:00:00 pm 17:00:00 pm
Please kindly advise 🙂
Replace your
forloop with:Admittedly a simple approach. I’m sure improvements are possible, but leave them as an exercise for the reader! 😉