title: How to get number of weeks per month in PHP?
body: <p>I am currently generating an html table which contains dates in it's header</p>
  
  <p>Those dates are the supposedly first days of each week through the whole year (the displayed year may change according to a filter), and they are generated by the following code</p>
  
  <pre><code>&lt;?php $firstDay = strtotime($year . 'W' . str_pad($week, 2, '0', STR_PAD_LEFT)); ?&gt;
  &lt;td&gt;&lt;?= date('d/m/Y', $firstDay) ?&gt;&lt;/td&gt;
  </code></pre>
  
  <p>This is inside a <code>for</code> loop that makes the <code>$week</code> go from <code>1</code> to the number of weeks in the whole year.</p>
  
  <p>What I have noticed is that for some years, the last monday of December from the previous year is included in there too, such as <code>29/12/2014</code> when I select <code>2015</code> in the filter.</p>
  
  <p>So far, to determine the number of weeks that are in a month, I used to count the number of mondays in said month programmatically and used it as the colspan for the row above the dates row, and so, there are uncounted weeks for january for some years, which makes the table shift.</p>
  
  <p>The rendered html for the year <code>2015</code> would look like this</p>
  
  <pre><code>&lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th colspan="4"&gt;January&lt;/th&gt;
              &lt;th colspan="4"&gt;February&lt;/th&gt;
              [...]
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;!-- Under January cell --&gt;
              &lt;th&gt;29/12/2014&lt;/th&gt;
              &lt;th&gt;05/01/2015&lt;/th&gt;
              &lt;th&gt;12/01/2015&lt;/th&gt;
              &lt;th&gt;19/01/2015&lt;/th&gt;
              &lt;!-- Under February cell --&gt;
              &lt;th&gt;26/01/2015&lt;/th&gt;
              &lt;th&gt;02/02/2015&lt;/th&gt;
              [...]
          &lt;/tr&gt;
      &lt;/thead&gt;
      [...]
  </code></pre>
  
  <p>And after searching and testing a lot of week per month counters, none of those that I found seem to include that last monday from the previous year.</p>
  
  <p>Could anyone find out how to count the weeks including that extra week from the previous year when it occurs?</p>