0

I want to iterate between two dates in php. This is my code:

for($y=date("01/01/2017");$y<=date("01/06/2017");$y++){
    echo $y . "<br>";
}

Expected output:

01/01/2017
02/01/2017
03/01/2017
04/01/2017
05/01/2017
06/01/2017...... like this till 01/06/2017

Actual output:

01/01/2017
01/01/2018
01/01/2019
01/01/2020
01/01/2021
01/01/2022....

How do I get the correct dates?

EDIT: The dates should be in dd/mm/yyyy format

  • Does this answer your question? [Adding days to $Date in PHP](https://stackoverflow.com/questions/3727615/adding-days-to-date-in-php) – AaronJ Aug 22 '21 at 13:36

1 Answers1

1

Does this work for you?

$date = DateTime::createFromFormat("d-m-Y", '01-01-2017');
$endDate = DateTime::createFromFormat("d-m-Y", '01-06-2017');

while ($date <= $endDate) {
    echo $date->format("d/m/Y") . "\n";
    $date->modify('+1 day');
}

I tested this at http://sandbox.onlinephpfunctions.com/code/461ae543a882cf8d5e933a4ddc27e8e907f74e01

AaronJ
  • 1,060
  • 10
  • 24