Get number of weekdays in a given month

后端 未结 12 2199
无人及你
无人及你 2020-12-03 15:56

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 ?

12条回答
  •  长情又很酷
    2020-12-03 16:14

    This is the simplest code I could come up with. You really would need to create an array or a database table to hold the holidays to get a true, "Working Days" count, but that wasn't what was asked, so here you go, hope this helps someone.

    function get_weekdays($m,$y) {
    $lastday = date("t",mktime(0,0,0,$m,1,$y));
    $weekdays=0;
    for($d=1;$d<=$lastday;$d++) {
        $wd = date("w",mktime(0,0,0,$m,$d,$y));
        if($wd > 0 && $wd < 6) $weekdays++;
        }
    return $weekdays;
    }
    

提交回复
热议问题