How to calculate the number of weekdays in the current year with PHP

1 Answer

0 votes
function countWeekdaysInYear($year) {
    $weekdayCount = 0;

    $date = new DateTime("$year-01-01");
    while ($date->format("Y") == $year) {
        if ($date->format("N") < 6) { // 1 (Monday) to 5 (Friday)
            $weekdayCount++;
        }
        $date->modify('+1 day');
    }

    return $weekdayCount;
}
        
$year = date("Y");
$weekdayCount = countWeekdaysInYear($year);
         
echo "Number of weekdays in $year: $weekdayCount\n";



/*
run:

Number of weekdays in 2025: 261

*/

 



answered Feb 18, 2025 by avibootz

Related questions

1 answer 154 views
1 answer 80 views
2 answers 85 views
1 answer 104 views
2 answers 77 views
1 answer 101 views
1 answer 137 views
...