PHP Day迭代器 [英] PHP Day iterator
本文介绍了PHP Day迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以使用以下代码显示从当前日期到未来3个月的天数:
I'm able to show days from current date to next 3 months with this code:
$begin = new DateTime();
$end = new DateTime(date('Y-m-d', strtotime('+3 months', strtotime(date("d-m-Y")))));
$interval = DateInterval::createFromDateString('1 day');
$days = new DatePeriod($begin, $interval, $end);
foreach ( $days as $day ) {
...
}
我认为代码可以缩短,尤其是对于 $ end
。您能帮忙吗?
I feel the code can be shortened especially for $end
. Could you help?
哦,我也想得到前三个月的时间。我将‘+ 3个月’更改为
,但是没有运气。有想法吗?
Oh, I also want to get previous 3 months. I changed '+3 months'
to '-3 months'
but no luck. Any ideas?
推荐答案
对于第一个问题,您可以直接写
For your first question, you can directly write
$end = new DateTime('+3 months');
要返回3个月,请使用3个月前的
而不是 -3个月
。
To go back 3 months, use3 months ago
instead of -3 months
.
这篇关于PHP Day迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文