Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get number of weekdays in a given month

I want to calculate the number of weekdays days in a give month and year. Weekdays means monday to friday. How do i do it ?

like image 266
Hacker Avatar asked Dec 06 '11 07:12

Hacker


1 Answers

You don't need to count every day in the month. You already know the first 28 days contain 20 weekdays no matter what. All you have to do is determine the last few days. Change the start value to 29. Then add 20 weekdays to your return value.

function get_weekdays($m,$y) {
$lastday = date("t",mktime(0,0,0,$m,1,$y));
$weekdays=0;
for($d=29;$d<=$lastday;$d++) {
    $wd = date("w",mktime(0,0,0,$m,$d,$y));
    if($wd > 0 && $wd < 6) $weekdays++;
    }
return $weekdays+20;
}
like image 132
l3rainzone Avatar answered Sep 23 '22 04:09

l3rainzone