Link to home
Start Free TrialLog in
Avatar of tonelm54
tonelm54

asked on

Get array of months between 2 dates

Im trying to get a list of months between 2 dates, and came up with:-

        function echoDate( $start, $end ){
            $current = $start;             $ret = array();             while( $current<$end ){                 $next = date('Y-M-01', $current) . "+1 month";                 $current = strtotime($next);                 $ret[] = date("Y-m",$current);             }             return $ret;         }

Open in new window

So, if I ran:-

echoDate(strtotime("2021-09-06"), strtotime("2021-12-26"));

Open in new window

I should get an array of:-

2021-09
2021-10
2021-11
2021-22

Open in new window

But instead I get an array of:-

2021-10
2021-11
2021-12
2022-01

Open in new window

Which I dont understand of where its getting these values from.


Any ideas?

ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial