How to know how many Sundays there are or the months of the year php mysql

Question:

Let's say the year is: 2016 I need to know how many Sundays are the months of the year and record and result in php variable. Reason: I need to pass a condition, example:

if ($dom_mes == 4 )
{     
    //CONDIÇÃO A     
} 
elseif ($dom_mes == 5 )
{
    //CONDIÇÃO B    
}
else
{
    //NADA A FAZER    
}

Answer:

It can take month by month. I modified the example to make a loop:

<?php
$array = [];
$ano = 2016;


for($i = 1; $i <= 12; $i++){
    $data = $ano . '-' . $i. '-01';

    $inicio = new DateTime($data);
    $fim = new DateTime($inicio->format('Y-m-t'));
    $dias = $inicio->diff($fim, true)->days;

    $array[$i] = intval($dias / 7) + ($inicio->format('N') + $dias % 7 >= 7);
}
?>
<pre>
<?=print_r($array) ?>
</pre>
Scroll to Top