title: PHP Build calendar that loops through until current month
body: <p>I have a project im working on while learning PHP. I need to create a page that displays a link for a year and the months of that year on one line. Then it breaks and goes to the next year. Continuing this until the current year stopping at the current year and current month. Something like this:</p>
  
  <p><code>2017 January February March April May June July August...</code></p>
  
  <p><code>2018 January February March April May June July</code></p>
  
  <p>Each year and month is a link to another page that will display results, sort of like an archive of blog posts, but not in WordPress.</p>
  
  <p>I have been able to loop through all previous years using a while loop conditional that it is not the current year. I was able to get the current year to display correctly, but I had to split this into 2 separate sets of code that essentially have the same HTML code 2 times. It's the months calculation that changes. I would like to build this into just one set of instructions that if changed in the future I only have to change things once. 2 times is not bad, but it can be streamlined.</p>
  
  <p>I was able to loop through and get all years and all months (but I only want up to the current month of the year), and I was able to loop through all years with only the current month (July) displaying for each year (August forward are missing for all years)</p>
  
  <p>Are there methods that you would use to complete this? Start with an <code>IF</code> statement and move to a while loop where I want the full months, and a <code>FOREACH</code> on the months that I want it to stop at?</p>
  
  <p>I know I'm going to need to use <code>strtotime</code> and <code>mktime</code> methods, but what structure would you use here?</p>
  
  <p>I have been working on this for a few weeks and I feel like an idiot for not being able to figure out something that seems so basic. All of my searches result in people asking about calendars, mktime, strtotime, etc. and it doesn't seem to fit my answer for me to figure this out.</p>
  
  <p>Here is the code that I have that works, but I would like to clean it up a bit:</p>
  
  <pre><code>//Set year and month variables
  $startYear = '2014';
  $endYear = date('Y');
  $incrementYear = $startYear;
  $startMonth = '01';
  $endMonth = date('m');
  $dataValue = $startMonth;
  
  // Start by laying out all previous years
  while ($incrementYear != $endYear) {
      echo '&lt;tr&gt;&lt;td&gt;&lt;a href="/Archive/view/' . $incrementYear . '"&gt;' . $incrementYear . '&lt;/a&gt;&lt;/td&gt;';
      for ($x = 1; $x &lt;= 12; $x++) {
          $dataValue = $x;
          echo '&lt;td class="month sp-month" data-value="' . $dataValue . '" data-id="' . $incrementYear . '"&gt;&lt;a href="/Archive/view/' . $incrementYear . '/' . date('m', mktime(0, 0, 0, $x, 1)) . '"&gt;' . date('F', mktime(0, 0, 0, $x, 1)) . ' (' . rand(5, 100) .'&lt;/a&gt;&lt;/td&gt;';
      }
      $incrementYear++;
  }
  
  //Create the current year's line, incrementing only until the current month
  if ($incrementYear == $endYear) {
      echo '&lt;tr&gt;&lt;td&gt;&lt;a href="/Archive/view/' . $incrementYear . '"&gt;' . $incrementYear . '&lt;/a&gt;&lt;/td&gt;';
      foreach (range($startMonth, $endMonth) as $month) {
  
          // Set format for output
          $time = mktime(0, 0, 0, $month, 1, date('Y'));
          $dataValue = date('n', $time);
          $monthNum = date('m', $time);
          $monthName = date('F', $time);
  
          echo '&lt;td class="month" data-value="' . $dataValue . '" data-id="' . $incrementYear . '"&gt;&lt;a href="/Archive/view/' . $incrementYear . '/' . $monthNum . '"&gt;' . $monthName . '&lt;/a&gt;&lt;/td&gt;';
      }
  }
  </code></pre>
  
  <p>The output:</p>
  
  <p><code>2014 January Febuary March April May June July August September October November December</code>
  <code>2015 January Febuary March April May June July August September October November December</code>
  <code>2016 January Febuary March April May June July August September October November December</code>
  <code>2017 January Febuary March April May June July August September October November December</code>
  <code>2018 January Febuary March April May June July</code></p>
  
  <p>This is what I want, but the code is what I want to clean up. Instead of having 2 sets of </p>
  
  <p><code>echo '&lt;tr&gt;&lt;td&gt;&lt;a href="/Archive/view/' . $incrementYear . '"&gt;' . $incrementYear . '&lt;/a&gt;&lt;/td&gt;';</code></p>
  
  <p>and</p>
  
  <p><code>echo '&lt;td class="month" data-value="' . $dataValue . '" data-id="' . $incrementYear . '"&gt;&lt;a href="/Archive/view/' . $incrementYear . '/'</code></p>
  
  <p>I want to be able to put those into one each.</p>
  
  <p>Thanks!</p>