Link to home
Create AccountLog in
Avatar of APD Toronto
APD TorontoFlag 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.
SOLUTION
Avatar of Heather Ritchey
Heather Ritchey
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.