PHP-循环中的增量日期 [英] PHP - Increment Dates in Loop

查看:242
本文介绍了PHP-循环中的增量日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$end_date = new DateTime($_GET['end_date']); 
$last_day_this_month = $end_date->format('d-m-Y'); //outputs 10-03-2015

$start_date = new DateTime($_GET['start_date']);
$loop_dates = $start_date->format('d-m-Y'); //outputs 22-04-2015

        for($i = $loop_dates; $i <= $last_day_this_month; $i++)
        {
                echo $i;echo '<br>';


        }

在循环以下使用年份而不是年份日期

Using below loop increments the year and not dates

for($i = $loop_dates; $i <= $last_day_this_month; $i++)

如何使用循环遍历/递增开始/结束日期,以便输出中的所有日期2015年10月3日 2015年4月22日

How could I traverse/increment through start/end dates using loop so that it outputs all the dates from 10-03-2015 to 22-04-2015.

PS:我我正在使用 PHP5.3 ,因此想采用面向对象的方法,而不是使用strtotime

P.S: I am using PHP5.3 and so want to go with object oriented approach instead of using strtotime

推荐答案

您可以尝试以下代码

$begin = new DateTime('2013-02-01');
$end = new DateTime('2013-02-13');

$daterange = new DatePeriod($begin, new DateInterval('P1D'), $end);

foreach($daterange as $date){
    echo $date->format("d") . "<br>";
}

这篇关于PHP-循环中的增量日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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