Copyright WebTechGlobal © 2016
HomeWTG BlogPHP Function Get Previous Twelve Months From Current Month

PHP Function Get Previous Twelve Months From Current Month

PHP Function Get Previous Twelve Months From Current Month

Working on WP e-Customers right now and adding some charts. A bar graph displaying registrations from the last twelve month is the first. I quickly came up with a way to establish 12 months backwards.

                    <?php
                    $months_array = array('january','february','march','april','may','june','july','august','september','october','november','december');
                    $months_array_reversed = array_reverse($months_array,true);

                    $reached_this_month = false;# once true it indicates the foreach has arrived at the current month
                    $months_past = array();# our final array of months

                    // loop to the current month then start adding all remaining months to our $months_past array
                    foreach($months_array_reversed as $key => $month){

                        // only add months to our final array once we have already reached the current month
                        if($reached_this_month){
                            $months_past[]['month'] = $key;
                            $months_past[]['monthname'] = $month;
                            $months_past[]['year'] = date('Y');
                        }

                        // when we reach current month
                        if($key == date('n')){
                            $reached_this_month = true;
                        }
                    }

                    // add remaining months starting from December
                    if(date('n') != 12){
                        foreach($months_array_reversed as $key => $month){
                            $months_past[]['month'] = $key;
                            $months_past[]['monthname'] = $month;
                            $months_past[]['year'] = date('Y') - 1;

                            // once we have twelve months we are done
                            if(count($months_past) == 12){break;}
                        }
                    }
                    ?>
Share With:
Rate This Article
Author
  • dibbz

    foreach (range(0, -11) as $month) {
    echo date(“F”, strtotime(“${month} month”)).”\n”;
    }

    • Ryan Bayne

      That is good actually welcome to WebTechGlobal. What I done was the first thing to cross my my mind and but I must admit. I didn’t consider trying a minus value in any way at all so thanks for that, I best remember to play around with minus more often.

      However I’ll run with what I have for a moment as the array is the important part. It will be used in various ways eventually so my approach is to add a lot of values to it now. In risking confusing the post. My use may go back more than 12 months and so I also need to track how far back a month is.

  • Ryan Bayne

    After posting this code I realized that when my script gets to a point where I need to query data. I will need to query a month within a specific year and so I extended the $months_past array to include a month and year value per key. Just do date(‘Y’) – 1 within the second foreach to establish the previous year.