PHP将45分钟添加到带有循环的时间中 [英] PHP add 45 Minutes to a Time with Loop
问题描述
我正在准备用于输入访问开始时间和结束时间(如09:00和20:00)的约会脚本,每个访问时间为45分钟.
I'm preparing an appointment script for entering visiting start hours and end hours like 09:00 and 20:00 and each visit period is 45 minutes.
我想在循环中从开始时间到结束时间增加45分钟.但是我无法通过互联网上的样本来做.
I want to add 45 minutes from start time to end time within a loop. But with samples on internet I couldn't do it.
访问时间是动态的,我的意思是可能是09:00-20:00或11:00-23:00等.
Visiting times are dynamic I mean it could be 09:00-20:00 or 11:00-23:00 etc.
我要尝试的确切解决方案是:
The exact solution I'm trying to do is:
$visit_start = '09:00';
$visit_end = '20:00';
$difference = $visit_end - $visit_start;
$i = 1;
while( $i <= $difference )
{
print $visit_hour_list = $visit_start + 45;
$i++;
}
,打印结果如下:
09:00
09:45
10:30
11:15
12:00
直到小时结束.但是我不知道那怎么工作.
until to end hour. But I have no clue how that could work.
推荐答案
在类似的问题已回答 href ="https://stackoverflow.com/q/8990941/367456"> 如何在php中获取日期范围?" ,您可以使用DatePeriod
类在开始时间和结束时间之间的每45分钟时间内进行迭代:
As already answered in the similar question "How can I get a range of dates in php?", you can make use of the DatePeriod
class in PHP to iterate over each 45 minute times' between a start and end time:
演示:
<?php
/**
* PHP add 45 Minutes to a Time with Loop
*
* @link https://stackoverflow.com/a/19399907/367456
*/
$begin = new DateTime("09:00");
$end = new DateTime("20:00");
$interval = DateInterval::createFromDateString('45 min');
$times = new DatePeriod($begin, $interval, $end);
foreach ($times as $time) {
echo $time->format('H:i'), '-',
$time->add($interval)->format('H:i'), "\n"
;
}
输出:
09:00-09:45
09:45-10:30
10:30-11:15
11:15-12:00
12:00-12:45
12:45-13:30
13:30-14:15
14:15-15:00
15:00-15:45
15:45-16:30
16:30-17:15
17:15-18:00
18:00-18:45
18:45-19:30
19:30-20:15
这篇关于PHP将45分钟添加到带有循环的时间中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!