creating a loop for time incremented by 15 minutes

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

    DateInterval can be used to create a new dateinterval object for our date calculation and uses in any script. In the advance PHP object oriented style for all date & time calculations this format is useful.

    Check below links:
    http://php.net/manual/en/class.dateinterval.php
    http://www.plus2net.com/php_tutorial/date-interval.php

    0 讨论(0)
  • 2020-12-29 13:34

    my simple logic here

       $start=strtotime('00:00');
       $end=strtotime('23:30');
    
        for ($i=$start;$i<=$end;$i = $i + 15*60)
        {
    
         //write your if conditions and implement your logic here
    
         echo date('g:i A',$i).'<br>';
    
        }
    

    in loop you can play what you want

    0 讨论(0)
  • 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)

    0 讨论(0)
  • 2020-12-29 13:45

    With PHP 5 >= 5.3.0 you can use DateTime::add, see: http://www.php.net/manual/de/datetime.add.php

    0 讨论(0)
  • 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))."<br>" : "";
            }
    }
    

    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

    0 讨论(0)
  • 2020-12-29 13:50

    Looks unnecessarily complicated to me. The following will print out what you want. Presumably it can be adapted for use in your code. Sorry about the messy end-condition.

    $min=array("00","15","30","45");
    
    for($i=8;$i<17;$i++)
      foreach ($min as $v)
        print "$i:$v\n";
    print "17:00\n";
    

    Or, if you want to do this in a slightly more opaque way...

    for($i=8*60;$i<=17*60;$i+=15)
      print floor($i/60) . ":" . ($i/60-floor($i/60))*60 . "\n";
    

    The above calculates a minutes value for 8 o'clock and then adds fifteen minutes repeatedly. You then use some math to extract hours and minutes from the running variable.

    0 讨论(0)
提交回复
热议问题