Get number of weekdays in a given month

后端 未结 12 2117
无人及你
无人及你 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;
    }
    
    0 讨论(0)
  • 2020-12-03 16:20

    Get the number of working days without holidays between two dates :

    Use example:

    echo number_of_working_days('2013-12-23', '2013-12-29');
    

    Output:

    3
    

    Link to the function

    0 讨论(0)
  • 2020-12-03 16:21

    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;
    }
    
    0 讨论(0)
  • 2020-12-03 16:21

    this will work

    // oct. 2013
    $month = 10;
    
    // loop through month days
    for ($i = 1; $i <= 31; $i++) {
    
        // given month timestamp
        $timestamp = mktime(0, 0, 0, $month, $i, 2012);
    
        // to be sure we have not gone to the next month
        if (date("n", $timestamp) == $month) {
    
            // current day in the loop
            $day = date("N", $timestamp);
    
            // if this is between 1 to 5, weekdays, 1 = Monday, 5 = Friday
            if ($day == 1 OR $day <= 5) {
    
                // write it down now
                $days[$day][] = date("j", $timestamp);
            }
        }
    }
    
    // to see if it works :)
    print_r($days);
    
    0 讨论(0)
  • 2020-12-03 16:23

    Some basic code:

    $month = 12;
    $weekdays = array();
    $d = 1;
    
    do {
        $mk = mktime(0, 0, 0, $month, $d, date("Y"));
        @$weekdays[date("w", $mk)]++;
        $d++;
    } while (date("m", $mk) == $month);
    
    print_r($weekdays);
    

    Remove the @ if your PHP error warning doesn't show notices.

    0 讨论(0)
  • 2020-12-03 16:23

    Calculate working days in a month from any date:

    public function getworkd($mday)
    {
        $dn = new DateTime($mday);
        $dfrom = $dn->format('Y-m-01');
        $dtill = $dn->format('Y-m-t');
        $df = new DateTime($dfrom);
        $dt = new DateTime($dtill);
        $wdays = 0;
        while($df<=$dt)
        {
            $dof= $df->format('D') ;
            if( $dof == 'Sun' || $dof == 'Sat' ) ; else $wdays++;
            $df->add(new DateInterval('P1D'));
        }
        return $wdays;
    }
    
    0 讨论(0)
提交回复
热议问题