php 为时间增加 15 分钟创建一个循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6949035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:39:52  来源:igfitidea点击:

creating a loop for time incremented by 15 minutes

phpdatetime

提问by scarhand

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

我需要它从 08:00 到 17:00

heres 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:

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

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

采纳答案by Gazler

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

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

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)

我会考虑在一天中的所有小时内执行您的 sql 查询,然后从中挑选出那些,否则您将每隔 15 分钟进行一次 sql 查询(使用您的示例数据进行 37 次查询)

回答by Alan Bell

it can also be done with the range function

也可以用 range 函数来完成

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

所以你没有循环,它只是为你创建一个数组(我的循环只是在格式化的同时打印出来)

回答by Richard

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.

以上计算8点钟的分钟值,然后重复添加十五分钟。然后使用一些数学运算从运行变量中提取小时和分钟。

回答by Raja Ram T

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

在循环中你可以玩你想要的

回答by Navid Vakili

DateIntervalcan be used to create a new dateintervalobject 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.

DateInterval可用于dateinterval为我们的日期计算创建一个新对象并在任何脚本中使用。在用于所有日期和时间计算的高级 PHP 面向对象样式中,这种格式很有用。

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

检查以下链接:
http: //php.net/manual/en/class.dateinterval.php
http://www.plus2net.com/php_tutorial/date-interval.php

回答by scube

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

使用 PHP 5 >= 5.3.0,您可以使用DateTime::add,请参阅:http: //www.php.net/manual/de/datetime.add.php

回答by Jazzy

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

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

回答by Joost

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

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

将该行移动到最外面的for-loop 之后。