-2

I would like to get remaining date in PHP. For example, I would like to get date list from 2017-07-01 to 2017-08-10

So, I would like the output going to be like this

2017-07-01
2017-07-02
2017-07-03
2017-07-04
......
2017-08-10

How could I do that anyway? Thank you

Bobby
  • 67
  • 2
  • 8

2 Answers2

1

Here is the working code for you: https://eval.in/842849

You should use DatePeriod which takes start-date, date interval, and end-date as arguments.

You will get the result object, which you can loop thru to get the desired dates between the 2 dates:

<?php
$begin = new DateTime('2017-07-01 ');
$end = new DateTime('2017-08-10');

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

foreach($daterange as $date){
    echo $date->format("Y-m-d") . "\n";
}

?>
Milan Chheda
  • 8,159
  • 3
  • 20
  • 35
1

You could also take a look at the DatePeriod class:

$period = new DatePeriod(
     new DateTime('2010-10-01'),
     new DateInterval('P1D'),
     new DateTime('2010-10-05')
);

Which should get you an array with DateTime objects.

Khaled Ouertani
  • 316
  • 2
  • 13