Given two dates what is the best way of finding the number of weekdays in PHP?

前端 未结 4 1667
自闭症患者
自闭症患者 2021-01-24 08:53

The title is pretty much self explanatory. Given two dates what is the best way of finding the number of week days using PHP? Week days being Monday to Friday.

For insta

4条回答
  •  鱼传尺愫
    2021-01-24 09:49

            $datefrom = strtotime($datefrom, 0);
            $dateto = strtotime($dateto, 0);
    
            $difference = $dateto - $datefrom;
    
            $days_difference = floor($difference / 86400);
            $weeks_difference = floor($days_difference / 7); // Complete weeks
    
            $first_day = date("w", $datefrom);
            $days_remainder = floor($days_difference % 7);
    
            $odd_days = $first_day + $days_remainder; // Do we have a Saturday or Sunday in the remainder?
            if ($odd_days > 7) { // Sunday
                $days_remainder--;
            }
            if ($odd_days > 6) { // Saturday
                $days_remainder--;
            }
    
            $datediff = ($weeks_difference * 5) + $days_remainder;
    

    From here: http://www.addedbytes.com/php/php-datediff-function/

提交回复
热议问题