Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating a loop for time incremented by 15 minutes

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
...etc

I need it to go from 08:00 to 17:00.

Here's my code so far:

function echo_datelist ($i, $j, $day, $month, $year)
{
    $time = str_pad($i, 2, '0', STR_PAD_LEFT).':'.str_pad($j, 2, '0', STR_PAD_LEFT);            
    $date = strtotime("$month $day $year $time:00");
    $sql = mysql_query("select b.room_type, c.name from bookings as b, customers as c where b.the_date='$date' and b.id_customer=c.id");
    
    echo $time.'<br />';
}

for ($i = 8; $i <= 16; $i++)
{
    for ($j = 0; $j <= 45; $j+=15)
        echo_datelist($i, $j, $day, $month, $year);
    
    echo_datelist(17, 0, $day, $month, $year);
}

The problem is, it is outputting a 17:00 in between each hour, example:

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

p.s. I know I shouldn't be making iterated trips to the database, but I'll solve that problem after this one.

like image 908
scarhand Avatar asked Aug 04 '11 21:08

scarhand


4 Answers

it can also be done with the range function

<?php
date_default_timezone_set("Europe/London");
$range=range(strtotime("08:00"),strtotime("17:00"),15*60);
foreach($range as $time){
        echo date("H:i",$time)."\n";
}
?>

so you don't have a loop, it just makes an array for you (my loop is just to print it out whilst formatting it)

like image 160
Alan Bell Avatar answered Oct 23 '22 02:10

Alan Bell


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

like image 33
Raja Ram T Avatar answered Oct 23 '22 01:10

Raja Ram T


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.

like image 12
Richard Avatar answered Oct 23 '22 00:10

Richard


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)

like image 8
Gazler Avatar answered Oct 23 '22 02:10

Gazler