PHP

Calculate the total number of weeks in a specified month

<?php
  /**
   * Return the total number of weeks of a given month.
   * @param int $year
   * @param int $month
   * @param int $start_day_of_week (0=Sunday ... 6=Saturday)
   * @return int
   */
  function weeks_in_month($year, $month, $start_day_of_week)
  {
    // Total number of days in the given month.
    $num_of_days = date("t", mktime(0,0,0,$month,1,$year));
 
    // Count the number of times it hits $start_day_of_week.
    $num_of_weeks = 0;
    for($i=1; $i<=$num_of_days; $i++)
    {
      $day_of_week = date('w', mktime(0,0,0,$month,$i,$year));
      if($day_of_week==$start_day_of_week)
        $num_of_weeks++;
    }
 
    return $num_of_weeks;
  }
 
  // Testing:
  $year = 2014;
  for($i=1; $i<=12; $i++)
  {
    echo "$year-$i=>".weeks_in_month($year, $i, 1).'<br />';
  }
 
  // Output
  // 2014-1=>4
  // 2014-2=>4
  // 2014-3=>5
  // 2014-4=>4
  // 2014-5=>4
  // 2014-6=>5
  // 2014-7=>4
  // 2014-8=>4
  // 2014-9=>5
  // 2014-10=>4
  // 2014-11=>4
  // 2014-12=>5  
 
?>