您如何每30分钟循环一次 [英] How would you loop every 30 minutes

查看:109
本文介绍了您如何每30分钟循环一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试创建一个for循环,该循环在data-time HTML字段中输出HHMM.

Trying to make a for loop that has the HHMM outputted in a data-time HTML field.

我目前有:

for ($i = 1; $i <= 20; ++$i) {
    echo '<div class="mb-timer-hour"><div class="mb-timer-half" data-time="0830"></div></div>';
}

除了每次都会输出data-time="0830".

然后我尝试了$h = 30; $h + 30,但是随后执行了:0800, 0830, 0860, 0890, ...

I then tried $h = 30; $h + 30 but this then goes: 0800, 0830, 0860, 0890, ...

是否可以循环0800, 0830, 0900, 0930, ...?

开始时间和结束时间是用户变量,这就是为什么我没有对数组进行硬编码的原因.

The start time and end times are user variables which is why I haven't hard coded an array.

我希望的输出是:


<div class="mb-timer-hour"><div class="mb-timer-half" data-time="0830"></div></div>
<div class="mb-timer-hour"><div class="mb-timer-half" data-time="0930"></div></div>
<div class="mb-timer-hour"><div class="mb-timer-half" data-time="1030"></div></div>
...
<div class="mb-timer-hour"><div class="mb-timer-half" data-time="1730"></div></div>

推荐答案

您可以使用DateTime对象,并始终添加30分钟(或60分钟),直到达到结束时间为止.循环中使用format方法来输出时间.

You can use DateTime objects and always add 30 minutes (or 60 minutes) until the end time is reached. The format method is used in the loop to output the times.

$start = '0830';
$end = '1230';
$date_end = date_create($end);

for($date = date_create($start);$date <= $date_end; $date->modify('+30 Minutes')){

  echo '<div class="mb-timer-hour"><div class="mb-timer-half" data-time="'.$date->format('Hi').'"></div></div>';
}

循环中的HTML输出不是很好.我从问题中拿走了它.如果测试在浏览器中运行,则必须查看页面源文本以获取结果.

The HTML output in a loop is not nice. I took it from the question. If the test runs in the browser, the page source text must be viewed for the result.

这篇关于您如何每30分钟循环一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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