title: PHP: Loop through all weeks in a date range (last 2 years)?
body: <p>I like to iterate through all weeks in a date range that spawns 2 years. Starting at the current week number two years ago, until the current week number this year.</p>
  
  <p>The problem is that a year can either have 52 weeks or 53 weeks, for example:</p>
  
  <ul>
  <li><p>2015 had 53 weeks (the 53th was from 2015-12-28 till 2016-01-03)</p></li>
  <li><p>2016 had 52 weeks (the 52th was from 2016-12-26 till 2017-01-01)</p></li>
  </ul>
  
  <p>So this is currently my php code:</p>
  
  <pre><code># start with the current week 2 years ago
  $year = date("Y") - 2;
  $week = (int) date("W");    // (int) removes leading zero for weeks &lt; 10
  
  $endYear = date("Y");
  $endWeek = (int) date("W");
  
  # iterate through all weeks until now
  do {
      echo $week. " of ". $year;
  
      $week++;
      if ($week &gt; 52) { // or greater 53 ?????????????
          $year ++;
          $week = 1;
      }
  }
  while ($year &lt; $endYear || $week &lt; $endWeek);
  </code></pre>