Avatar of APD Toronto
APD Toronto
Flag for Canada asked on

Looping through Dates in PHP

Hi Experts,

How can I loop in between 2 dates?

For example, I have the following code:
//Loop through DR
        $date = new DateTime();
        $date_from = new DateTime($from); //'2015-08-01'
        $date_to = new DateTime($to); //'2015-08'14'
        $date_optput = '';
        $date_compare = '';
        
        for ($date = $date_from; $date <= $date_to; $date++):
            
            $date_optput = $date->format('D-M-y');
            echo $date_optput . '<br>';
        endfor;

Open in new window


However, this crashes my browser, ad I'm guessing because it goes through every millisecond? Which is what I don't want.

In comments I added my test data, so I am expecting the following output
1-Aug-15
2-Aug-15
3-Aug-15
4-Aug-15
...
14-Aug-15
15-Aug-15

Open in new window


Any help will be appreciated.
PHP

Avatar of undefined
Last Comment
Julian Hansen

8/22/2022 - Mon
SOLUTION
Heather Ritchey

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Julian Hansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes