开发者

Print time in 15-minute increments between two times in the same day

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开发者_C百科
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.


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)


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.


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


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)


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


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


echo_datelist(17, 0, $day, $month, $year);

Move that line to just after the outermost for-loop.


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


Setting up a DateTime object with explicit bounds and its interval makes this process very clean. Remember to set the end time just passed what you want because the DatePeriod is "exclusive" of the end datetime.

Code: (Demo)

$period = new DatePeriod(
     new DateTime('08:00:00'),
     new DateInterval('PT15M'),
     new DateTime('17:00:01')
);

foreach ($period as $dt) {
    echo $dt->format("H:i") . "\n";
}

Output:

08:00
08:15
08:30
08:45
09:00
09:15
09:30
09:45
10:00
10:15
10:30
10:45
11:00
11:15
11:30
11:45
12:00
12:15
12:30
12:45
13:00
13:15
13:30
13:45
14:00
14:15
14:30
14:45
15:00
15:15
15:30
15:45
16:00
16:15
16:30
16:45
17:00


I came up with this which will print the time with whatever increase you want, and you can change the time to show you 13PM or 1PM

<?php
        $start=strtotime('00:00');
        $end=strtotime('23:30');
        $increase = 15; // How many minute to increase
        
        for ($i=$start;$i<=$end;$i = $i + $increase*60)
       {
        if(date('H', $i) > 12 ){ //Here we check if the hour is bigger than 12, then we just do math -12 which will result in hour, for example 13PM which is 13-12=1PM
          $hour = date('H', $i) - 12;
          echo $hour.date(':i A',$i);
          }else{
           echo date('H:i A',$i);
          }
        }
        

       ?>

The Previous code result

00:00 AM
00:15 AM
00:30 AM
00:45 AM
01:00 AM
01:15 AM
01:30 AM
01:45 AM
02:00 AM
02:15 AM
02:30 AM
02:45 AM
03:00 AM
03:15 AM
03:30 AM
03:45 AM
04:00 AM
04:15 AM
04:30 AM
04:45 AM
05:00 AM
05:15 AM
05:30 AM
05:45 AM
06:00 AM
06:15 AM
06:30 AM
06:45 AM
07:00 AM
07:15 AM
07:30 AM
07:45 AM
08:00 AM
08:15 AM
08:30 AM
08:45 AM
09:00 AM
09:15 AM
09:30 AM
09:45 AM
10:00 AM
10:15 AM
10:30 AM
10:45 AM
11:00 AM
11:15 AM
11:30 AM
11:45 AM
12:00 PM
12:15 PM
12:30 PM
12:45 PM
1:00 PM
1:15 PM
1:30 PM
1:45 PM
2:00 PM
2:15 PM
2:30 PM
2:45 PM
3:00 PM
3:15 PM
3:30 PM
3:45 PM
4:00 PM
4:15 PM
4:30 PM
4:45 PM
5:00 PM
5:15 PM
5:30 PM
5:45 PM
6:00 PM
6:15 PM
6:30 PM
6:45 PM
7:00 PM
7:15 PM
7:30 PM
7:45 PM
8:00 PM
8:15 PM
8:30 PM
8:45 PM
9:00 PM
9:15 PM
9:30 PM
9:45 PM
10:00 PM
10:15 PM
10:30 PM
10:45 PM
11:00 PM
11:15 PM
11:30 PM

If you don't need the 1PM 2PM method, you can just use same code and remove the math for subtracting, by removing the if method and replace it with

echo date('H:i A',$i);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜