创建一个循环,时间增加15分钟 [英] creating a loop for time incremented by 15 minutes

查看:232
本文介绍了创建一个循环,时间增加15分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个循环,将输出:

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

我需要从08:00到17:00

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

我的代码到目前为止:

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);
}

问题是,它每个小时之间输出一个17:00,例如:

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


推荐答案

您需要将外部for循环之外的最后一行移动。

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);

简单来说,你在说:

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

而不是:

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

我会考虑在一天中的所有时间执行你的sql查询,然后在时间内挑选一个从,否则你每隔15分钟做一个sql查询(37个查询与你的样本数据)

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)

这篇关于创建一个循环,时间增加15分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆