# Number of weeks in a month

Posted on

Coding Problem :

I would like to know how to calculate the number of weeks that have a month

`````` D  S  T  Q  Q  S  S
1    => Semana 1
2  3  4  5  6  7  8    => Semana 2
9 10 11 12 13 14 15    => Semana 3
16 17 18 19 20 21 22    => Semana 4
23 24 25 26 27 28 29    => Semana 5
30 31                   => Semana 6
``````

asked by anonymous 20.08.2014 / 14:41

Based on the logic for this other answer I did the function for this calculation.

The `date()` faults of PHP has #.

The difference compared to the other answer is that I only use the `DateTime` object here

``````function countSemanasMes (\$ano, \$mes) {

\$data = new DateTime("\$ano-\$mes-01");
\$dataFimMes = new DateTime(\$data->format('Y-m-t'));

\$numSemanaInicio = \$data->format('W');
\$numSemanaFinal  = \$dataFimMes->format('W') + 1;

// Última semana do ano pode ser semana 1
\$numeroSemanas = (\$numSemanaFinal < \$numSemanaInicio)
? (52 + \$numSemanaFinal) - \$numSemanaInicio
: \$numSemanaFinal - \$numSemanaInicio;

return \$numeroSemanas;

}
``````

DateTime considers the first day of the week to be Monday. If I wanted to be considered a different day, we can include a parameter for the first day of the week, as pointed out in the comments:

``````/**
* Calcula o número de semanas de um mês
*
* @param int \$ano
* @param int \$mes
* @param int \$primeiroDiaSemana Intervalo 1 (Segunda-Feira) até 7 (domingo), segundo ISO-8601
* @return int
*/
function countSemanasMes (\$ano, \$mes, \$primeiroDiaSemana = 7)
{
\$primeiroDiaMes = new DateTime("\$ano-\$mes-01");
\$ultimoDiaMes = new DateTime(\$primeiroDiaMes->format('Y-m-t'));

\$numSemanaInicio = \$primeiroDiaMes->format('W');
\$numSemanaFinal  = \$ultimoDiaMes->format('W') + 1;

// Última semana do ano pode ser semana 1
\$numeroSemanas = (\$numSemanaFinal < \$numSemanaInicio)
? (52 + \$numSemanaFinal) - \$numSemanaInicio
: \$numSemanaFinal - \$numSemanaInicio;

if (\$primeiroDiaMes->format('N') > \$primeiroDiaSemana)
\$numeroSemanas--;

if (\$ultimoDiaMes->format('N') < \$primeiroDiaSemana)
\$numeroSemanas--;

return \$numeroSemanas;
}
``````

According to your documentation , the `Date()` function allows the `W` parameter, which returns the number of the week of a given day.

Then, to calculate the number of weeks, subtract the Week Number from the last day of the month desired by the Week Number of the first.

I’m not a PHP programmer, but I’ve created a snippet that should do the calculation correctly for you.

``````<?php

\$date = new DateTime();
\$query_date = '2010-02-04';

\$source_date = strtotime(\$query_date);

\$dat_ini = new DateTime(date('Y-m-01', \$source_date));
\$dat_fin = new DateTime(date('Y-m-t', \$source_date));

\$NumeroSemanas = (int)\$dat_fin->format('W') - (int)\$dat_ini->format('W') + 1;
?>
``````

This is a problem that has several ways of implementing a solution. My idea would be to take the amount of days a month has, divide by seven (the number of days in a week) and get the whole value of that result.

Translating in code:

``````<?php
function getNumSemanas(\$mes, \$ano, \$diaInicialSemana = 'Sunday', \$calendario = CAL_GREGORIAN) {
\$diasEmUmMes = cal_days_in_month(\$calendario, \$mes, \$ano);
\$numSemanas = 1;
for (\$i=1; \$i<=\$diasEmUmMes; \$i++) {
if (date('l', strtotime("\$i-\$mes-\$ano")) === \$diaInicialSemana && !(\$i === 1 && date('l', strtotime("\$i-\$mes-\$ano")) === \$diaInicialSemana)) {
\$numSemanas++;
}
}

return \$numSemanas;
}
``````

The function below finds the number of weeks in a given month, assuming Monday as the first day of the week.

``````function weeks_in_month(\$month, \$year) {
// Start of month
\$start = mktime(0, 0, 0, \$month, 1, \$year);

// End of month
\$end = mktime(0, 0, 0, \$month, date('t', \$start), \$year);

// Start week
\$start_week = date('W', \$start);

// End week
\$end_week = date('W', \$end);

if (\$end_week < \$start_week) { // Month wraps
return ((52 + \$end_week) - \$start_week) + 1;
}

return (\$end_week - \$start_week) + 1;
}

echo '08/2014 tem: ' . weeks_in_month('08', '2014') . ' semanas';

// retorno
// 08/2014 tem: 5 semanas
``````

Here’s a possible solution:

``````/**
* Calcula a quantidade de Semanas em um mes
*
* @param string \$mesAno Mes e Ano no formato MM/AAAA. Ex.: 05/2014
* @return int Quantidade de semanas no mes
*/
{
\$data = '01/'.\$mesAno;
\$start = DateTime::createFromFormat('d/m/Y', \$data);

\$end = clone \$start;
\$end->sub(new DateInterval("P1D"));

return ceil((\$start->format('w') + \$end->format('d')) / 7);
}

``````