3
$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++)

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.

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

2
  • Have you considered DatePeriod? Commented Apr 21, 2015 at 7:06
  • calculate number of days between these two dates and apply $date->modify('+1 day'); change +1 with $i Commented Apr 21, 2015 at 7:09

2 Answers 2

8

You can try below code

$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>";
}
Sign up to request clarification or add additional context in comments.

1 Comment

It outputs 10-20 and not specifically from 10-MAR to 20-APR
3

Try this one

<?php
$start = '2013-08-25';
$end = '2013-08-29';
$datediff = strtotime($end) - strtotime($start);
$datediff = floor($datediff/(60*60*24));
for($i = 0; $i < $datediff + 1; $i++){
    echo date("Y-m-d", strtotime($start . ' + ' . $i . 'day')) . "<br>";
}
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.