creating a loop for time incremented by 15 minutes

后端 未结 8 2054
猫巷女王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:44

    You need to move the last line outside of the outer for loop.

    for ($i = 8; $i <= 16; $i++){
      for ($j = 0; $j <= 45; $j+=15){
        //inside the inner loop
        echo_datelist($i, $j, $day, $month, $year);
      }
      //inside the outer loop
    }
    //outside the outer loop
    echo_datelist(17, 0, $day, $month, $year);
    

    In plain terms, you are saying:

    For each hour between 8 and 16
      For each 15 minute interval
        Echo the time
      End
      Echo 17:00
    End
    

    Instead of:

    For each hour between 8 and 16
      For each 15 minute interval
        Echo the time
      End
    End
    Echo 17:00
    

    I would consider performing your sql query for all hours of the day and then picking out the ones within the time from, otherwise you be doing an sql query for each 15 minute interval (37 queries with your sample data)

提交回复
热议问题