creating a loop for time incremented by 15 minutes

后端 未结 8 2056
猫巷女王i
猫巷女王i 2020-12-29 13:23

i\'m trying to make a loop that will output this:

08:00
08:15
08:30
08:45
09:00
09:15
09:30
09:45

i need it to go from 08:00 to 17:00

8条回答
  •  情深已故
    2020-12-29 13:47

    i was working on a similar problem, but with the start/end times being changeable.

    this may need a little refinement, but i can't break it.

    all you need to supply in the beginning are the date and times.

    $day = "10/14/2011";
    
    $startTime = date(strtotime($day." 16:00"));
    $endTime = date(strtotime($day." 19:15"));
    
    $timeDiff = round(($endTime - $startTime)/60/60);
    
    $startHour = date("G", $startTime);
    $endHour = $startHour + $timeDiff; 
    
    for ($i=$startHour; $i <= $endHour; $i++)
    {
         for ($j = 0; $j <= 45; $j+=15)
            {
                    $time = $i.":".str_pad($j, 2, '0', STR_PAD_LEFT);
    
                    echo (date(strtotime($day." ".$time)) <= $endTime) ? date("g:i", strtotime($day." ".$time))."
    " : ""; } }

    outputs:

    4:00 4:15 4:30 4:45 5:00 5:15 5:30 5:45 6:00 6:15 6:30 6:45 7:00 7:15

提交回复
热议问题