Hello,

This is what I am trying to achieve. I have two dates, let us say they are 2005-10-07 and 2006-01-04. I want to create a function that will be able to print all the months between two those dates inclusively. So in this example with those two dates above, it should print: October, November, December and January (or 10, 11, 12, 1)

I hope I am clear enough but to make sure, here is another example.
Date1: 2005-02-20
Date2: 2005-06-05

The function should print out: February, March, April, May, and June (or 2, 3, 4, 5, 6).

Thank you and have a great weekend!

-Ren

    well you can create an array of the months first.

    then take in 2 dates into your function. take the earliest date and print al the months in a while loop until you get to the month of your second date. make sure to compare years first and then your months

      Mmmmm, convoluted functions for the win....

      Sorry, I was bored at work.

      function get_months($date1, $date2) {
         $time1  = strtotime($date1);
         $time2  = strtotime($date2);
         $my     = date('mY', $time2);
      
         $months = array(date('F', $time1));
      
         while($time1 < $time2) {
            $time1 = strtotime(date('Y-m-d', $time1).' +1 month');
            if(date('mY', $time1) != $my && ($time1 < $time2))
               $months[] = date('F', $time1);
         }
      
         $months[] = date('F', $time2);
         return $months;
      }

      print_r(get_months('2005-02-20', '2005-12-31'));

        It works like a charm. I was just going to start working on the function using an array as the first reply post suggested but I guess now I don't have to. Thank you so much, and have a super weekend!

        -Ren

          One problem when I tried this: print_r(get_months('2005-10-31', '2006-01-05'));

          I get Array ( [0] => October [1] => December [2] => January ) , no November. Any ideas why? It seems to work with the other dates I've tried so far.

          Thanks again,
          Ren

            Problems within strtotime would cause that. You can see it when running this:

            echo date("m/d/Y", strtotime("2005-10-31 +1 month"));

            It returns 12/01/2005

            It's really up to you at this point what to do. The suggestion for months within an array would be the best bet, but if you wanted to keep it based completely off of php's date and time functions, you could modify the increment and perform another check within this function.

            And you will have to forgive me for even posting it, since I was mainly just kind of screwing around. But something like this..

            function get_months($date1, $date2) {
               $time1  = strtotime($date1);
               $time2  = strtotime($date2);
               $my     = date('mY', $time2);
            
               $months = array(date('F', $time1));
               $f      = '';
            
               while($time1 < $time2) {
                  $time1 = strtotime((date('Y-m-d', $time1).' +15days'));
                  if(date('F', $time1) != $f) {
                     $f = date('F', $time1);
                     if(date('mY', $time1) != $my && ($time1 < $time2))
                        $months[] = date('F', $time1);
                  }
               }
            
               $months[] = date('F', $time2);
               return $months;
            }

              Thanks for the tip regarding the increment.

              The function works now. I just do a check if the day of the first date is < 31, then use +1 month, if it's 31, use +29 days.

              Thanks again,
              Ren

                2 years later

                Hi,
                I am also stuck at the same problem as renny, But renny can you provide the updated code of yours, where things actually started working fine..

                Thanks
                ichampin

                  2 years later

                  It works for me : )

                  function noOfMonths($date_1, $date_2)
                  {
                  list ($year_1, $month_1, $day_1) = explode("-", $date_1);
                  list ($year_2, $month_2, $day_2) = explode("-", $date_2);

                  $month = 0;
                  
                  if ($year_1 != $year_2)
                  {
                  	$month += ($year_2 - $year_1 - 1) * 12;
                  	$month += 12 - $month_1 + $month_2;
                  
                  } else
                  {
                  	$month = $month_2 - $month_1;
                  }
                  
                  return $month;

                  }
                  $start_date = "2010-09-01";
                  $end_date = "2011-08-01";

                  echo noOfMonths($start_date, $end_date)

                    9 months later

                    Hi,
                    this code is better:

                    function get_months($date1, $date2) { 
                       $time1  = strtotime($date1); 
                       $time2  = strtotime($date2); 
                       $my     = date('n-Y', $time2); 
                       $mesi = array(Gen,Feb,Mar,Apr,Mag,Giu,Lug,Ago,Set,Ott,Nov,Dic);
                    
                       //$months = array(date('F', $time1)); 
                       $months = array(); 
                       $f      = ''; 
                    
                       while($time1 < $time2) { 
                          if(date('n-Y', $time1) != $f) { 
                             $f = date('n-Y', $time1); 
                             if(date('n-Y', $time1) != $my && ($time1 < $time2)) {
                             	$str_mese=$mesi[(date('n', $time1)-1)];
                                $months[] = $str_mese." ".date('Y', $time1); 
                             }
                          } 
                          $time1 = strtotime((date('Y-n-d', $time1).' +15days')); 
                       } 
                    
                       $str_mese=$mesi[(date('n', $time2)-1)];
                       $months[] = $str_mese." ".date('Y', $time2); 
                       return $months; 
                    } 
                      Write a Reply...