title: getting all months between 2 dates
body: <p>I created a function that returns an array containing each month, starting from a supplied carbon date and ending on the current date.</p>
  
  <p>Although the function is doing what it is supposed to do, it looks hideous. Clearly my programming skills arent yet what they're supposed to be. Surely there must be a better way to achieve what i want.</p>
  
  <p>My code looks like this:</p>
  
  <pre><code>    class DateUtilities {
      protected $months = ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december'];
  
      public function getMonthListFromDate(Carbon $date)
      {
          $monthArray = array();
          $today = Carbon::today();
  
          $currentYear = $today-&gt;copy()-&gt;format('Y');
          $currentMonth = strtolower($today-&gt;copy()-&gt;format('F'));
  
          $startYear = $date-&gt;copy()-&gt;format('Y');
          $startMonth = strtolower($date-&gt;copy()-&gt;format('F'));
  
          for($i = $startYear; $i &lt;= $currentYear; $i ++) {
              foreach($this-&gt;months as $monthIndex =&gt; $month) {
                  if (($monthIndex &gt;= array_search($startMonth, $this-&gt;months) &amp;&amp; $i == $startYear) ||
                      ($monthIndex &lt;= array_search($currentMonth, $this-&gt;months) &amp;&amp; $i == $currentYear) ||
                      ($i != $startYear &amp;&amp; $i != $currentYear)) {
                      $formattedMonthIndex = ($monthIndex + 1);
  
                      if($formattedMonthIndex &lt; 10) {
                          $monthArray['0' . $formattedMonthIndex . '-' . $i] = $month . ' ' . $i;
                      } else {
                          $monthArray[$formattedMonthIndex . '-' . $i] = $month . ' ' . $i;
                      }
                  }
              }
          }
  
          return $monthArray;
      }
  }
  </code></pre>
  
  <p>and the result is:</p>
  
  <pre><code>array:25 [▼
    "03-2013" =&gt; "march 2013"
    "04-2013" =&gt; "april 2013"
    "05-2013" =&gt; "may 2013"
    "06-2013" =&gt; "june 2013"
    "07-2013" =&gt; "july 2013"
    "08-2013" =&gt; "august 2013"
    "09-2013" =&gt; "september 2013"
    "10-2013" =&gt; "october 2013"
    "11-2013" =&gt; "november 2013"
    "12-2013" =&gt; "december 2013"
    "01-2014" =&gt; "january 2014"
    "02-2014" =&gt; "february 2014"
    "03-2014" =&gt; "march 2014"
    "04-2014" =&gt; "april 2014"
    "05-2014" =&gt; "may 2014"
    "06-2014" =&gt; "june 2014"
    "07-2014" =&gt; "july 2014"
    "08-2014" =&gt; "august 2014"
    "09-2014" =&gt; "september 2014"
    "10-2014" =&gt; "october 2014"
    "11-2014" =&gt; "november 2014"
    "12-2014" =&gt; "december 2014"
    "01-2015" =&gt; "january 2015"
    "02-2015" =&gt; "february 2015"
    "03-2015" =&gt; "march 2015"
  ]
  </code></pre>
  
  <p>Can anyone help me improve this code?</p>
  
  <p>EDIT:</p>
  
  <p>After the great tips i ended up with the following:</p>
  
  <pre><code>class DateUtilities {
  
  public function getMonthListFromDate(Carbon $start)
  {
      $start = $start-&gt;startOfMonth();
      $end   = Carbon::today()-&gt;startOfMonth();
  
      do
      {
          $months[$start-&gt;format('m-Y')] = $start-&gt;format('F Y');
      } while ($start-&gt;addMonth() &lt;= $end);
  
      return $months;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Thank you for the help guys!!</p>