2

I need to get a range of hours between two given hours, with 15 minutes steps, so I get this:

"book_time_range": [
    "13:00-13:15",
    "13:15-13:30",
    "13:30-13:45",
    "13:45-14:00",
    "14:00-14:15",
    "14:15-14:30",
    "14:30-14:45",
    "14:45-15:00",
    "15:00-15:15",
    "15:15-15:30"
],

This is as far as I got:

public function hours_range_between_start_and_end(
    $start = '13:00', 
    $end = '15:30', 
    $step = 900, 
    $format = 'H:i'
) {
    $times_ranges = [];

    $start = strtotime($start) - strtotime('TODAY');
    $end = strtotime($end) - strtotime('TODAY');

    foreach (range($start, $end, $step) as $increment)
    {
        $increment = gmdate('H:i', $increment);

        list($hour, $minutes) = explode(':', $increment);

        $date = new DateTime($hour . ':' . $minutes);

        $times_ranges[] = $date->format($format);
    }

    return $times_ranges;
}

Which gives me:

"book_time_range": [
    "13:00",
    "13:15",
    "13:30",
    "13:45",
    "14:00",
    "14:15",
    "14:30",
    "14:45",
    "15:00",
    "15:15",
    "15:30"
],

That is technically correct, but I'm getting stuck on how to work with that array to get the one I want.

Any tips? Maybe I'm approaching this incorrectly from the beginning?

unclexo
  • 3,691
  • 2
  • 18
  • 26
  • The easiest would be to just loop over the items of your resulting array and append the next item (except the last one). Assuming that you just want to get the strings of time ranges. – Andreas May 28 '20 at 10:33
  • Yeah, I just need the string like "13:00-13:15". Gonna give it a try, thanks. – Miguel Heredia May 28 '20 at 10:37
  • 3
    Also, for a more elegant solution check out https://stackoverflow.com/a/4312630/6512170 – Andreas May 28 '20 at 10:41
  • Oh thanks, I'll check it. Closing this since I got what I wanted, not pretty at all, but while I check that link, it works. – Miguel Heredia May 28 '20 at 10:51

3 Answers3

2

You can achieve that by another simple way. Just set up start time and end time using DateTime::class and then add 15 minutes to the start time until you reach the end time with the help of a while-loop as the following:

<?php

$begin = new DateTime('2020-05-28 13:00');
$end = new DateTime('2020-05-28 15:30');

$timeRanges = [];
while($begin < $end) {

    $output = $begin->format('H:i') . " - ";
    $begin->modify('+15 minutes');          /** Note, it modifies time by 15 minutes */
    $output .= $begin->format('H:i');

    $timeRanges[] = $output;
}

print_r($timeRanges);

Check out the output here https://3v4l.org/r0E3R

unclexo
  • 3,691
  • 2
  • 18
  • 26
0

Following Andreas's advice, I took the first array and to each element I added the next one, besides concatenating the '-' in the middle, to get the result I wanted, deleting the last item of the array as there would be no more to add and it would give me a "15:30-" last item which I don't want.

foreach ($times_ranges as $key => $times_range) 
{
   $times_range_formatted[] = $times_range.'-'.next($times_ranges);
}

unset($times_range_formatted[count($times_range_formatted)-1]);

Result:

"book_time_range": [
            "13:00-13:15",
            "13:15-13:30",
            "13:30-13:45",
            "13:45-14:00",
            "14:00-14:15",
            "14:15-14:30",
            "14:30-14:45",
            "14:45-15:00",
            "15:00-15:15",
            "15:15-15:30"
        ],
-1
$times = array();
$start_time = strtotime("9:00am");
$end_time = strtotime("9:00pm");
for ($i = $start_time; $i <= $end_time; $i += 3600){
  $times[] = date("g:ia", $i);
}
return $times;