PHP时间间隔 [英] PHP time intervals
问题描述
我正在寻找的是设置一个开始时间,一个结束时间,然后在给定的时间间隔之间迭代一组次。例如,上午9:00 - 下午5:00是开始时间,在这些时间之间以半小时为间隔返回的值是9:30,10:00,10:30,11:00等。 / p>
下面是一些伪代码,因为我不确定我需要在这里插入什么(至少我没有尝试过) ($ i = *开始时间*; $ i = *结束时间*; $ i + = *时间间隔*){
echo $ i;
$ b $ p
$ b $ p
$ =h2_lin>解决方案
关闭我的头顶。
$ start = new DateTime('2013-08-14 09:00:00');
$ end = new DateTime('2013-08-14 17:00:00');
$ interval = new DateInterval('PT30M');
$ period = new DatePeriod($ start,$ interval,$ end);
$ b foreach($ period as $ dt)
{
//做某事
echo $ dt->格式('H:iA');
}
链接 I'm looking for a solution to something which seems like it should be pretty simple, but it doesn't seem that I can find any good answers on here, and I can't seem to get it to work myself. What I'm looking for is to set a start time, an end time, and then iterate through a set of times between given a time interval. Say, for example, 9:00 AM - 5:00 PM are the start times, and the values returned between these times at half hour intervals are 9:30, 10:00, 10:30, 11:00, etc. Here's some pseudo-code, because I'm not exactly sure what I need to plug in here (at least nothing I've tried has worked)... Any ideas on this? Off of the top of my head. You can tweak as necessary. Links 这篇关于PHP时间间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
for ($i = *start time*; $i = *end time*; $i += *time interval*) {
echo $i;
}
$start = new DateTime('2013-08-14 09:00:00');
$end = new DateTime('2013-08-14 17:00:00');
$interval = new DateInterval('PT30M');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt)
{
// do something
echo $dt->format('H:iA');
}