PHP时间间隔 [英] PHP time intervals

查看:252
本文介绍了PHP时间间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个解决方案,似乎它应该是非常简单的,但似乎并没有在这里找到任何好的答案,我似乎无法得到它自己的工作。



我正在寻找的是设置一个开始时间,一个结束时间,然后在给定的时间间隔之间迭代一组次。例如,上午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)...

for ($i = *start time*; $i = *end time*; $i += *time interval*) {
    echo $i;
}

Any ideas on this?

解决方案

Off of the top of my head. You can tweak as necessary.

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

Links

这篇关于PHP时间间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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