PHP: How many weekdays are in a month?

2013-07-09 PHP

For example – you want to know how many Mondays, Tuesdays etc. are in a specific month and year?

Here’s a simple code that returns an array with desired values.

$days_in_particular_month = cal_days_in_month(CAL_GREGORIAN, $your_date_month, $your_date_year);
$days_of_week_array = array();
for($i = 1; $i <= $days_in_particular_month; $i++)
{
  $day_of_week = date('l', mktime(0, 0, 0, $your_date_month, $i, $your_date_year));
  $days_of_week_array[$day_of_week]++;
}

As you can see, all you have to set are two variables your_date_month and your_date_year. When you’ll dump days_of_week_array you’ll see for example:

array(7) {
  ["Saturday"]=>
  int(5)
  ["Sunday"]=>
  int(5)
  ["Monday"]=>
  int(4)
  ["Tuesday"]=>
  int(4)
  ["Wednesday"]=>
  int(4)
  ["Thursday"]=>
  int(4)
  ["Friday"]=>
  int(4)
}

Example shown above is for July 2013. That means that there were four Mondays, Tuesdays, Wednesdays, Thursdays and Fridays as well as five Saturdays and Sundays.

Do NOT follow this link or you will be banned from the site!