Calculate number of working days in a month [dupli

2020-08-25 07:18发布

问题:

This question already has answers here:
Closed 7 years ago.

Possible Duplicate:
Get number of weekdays in a given month

How to calculate working days of any month? cal_days_in_month returns simply the total number of days in a month. My task is to calculate number of days in month except Saturdays and Sundays or Sundays alone. Any method to find this?

回答1:

function countDays($year, $month, $ignore) {
    $count = 0;
    $counter = mktime(0, 0, 0, $month, 1, $year);
    while (date("n", $counter) == $month) {
        if (in_array(date("w", $counter), $ignore) == false) {
            $count++;
        }
        $counter = strtotime("+1 day", $counter);
    }
    return $count;
}
echo countDays(2013, 1, array(0, 6)); // 23

The date function is used in this example. Note about ignore parameter: 0 is sunday, ..., 6 is saturday.



回答2:

copying second answer from : Get number of weekdays in a given month

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;
}

This should solve your problem and is an efficient solution indeed.



回答3:

A simple (not so elegant) answer would simply be to create a time stamp of the first day in the month, use date() to get the physical name of the month, then either switch or if to determine if it's a weekday or not.

$myTime = strtotime("1/1/2013");  // Use whatever date format you want
$daysInMonth = cal_days_in_month(CAL_GREGORIAN, 1, 2013); // 31
$workDays = 0;

while($daysInMonth > 0)
{
    $day = date("D", $myTime); // Sun - Sat
    if($day != "Sun" && $day != "Sat")
        $workDays++;

    $daysInMonth--;
    $myTime += 86400; // 86,400 seconds = 24 hrs.
}

echo "There are $workDays work days this month!";

Output

There are 23 work days this month!

http://php.net/manual/en/function.strtotime.php

http://php.net/manual/en/function.date.php



回答4:

Just do a Google search, man.

For posterity, your algorithm:

  • Get the number of days in the month using date().
  • Starting from day (1), use mktime/gmmktime to create an epoch timestamp, and then use date() to determine what day of the week it represents.
  • Repeat, until you've found the first working day (a Monday). Let this be "i".
  • Let the current tally be "t", whose initial value is (0).
  • Let j = i. Where j is less then (i + 5) -and- less then the maximum number of days in the month, increment t. Increment by (2) (to skip the weekend days) and repeat from the beginning of this line until "i" has exceeded the maximum number of days in the month.
  • Apply success and happiness, and repeat from beginning until total number of working days is (0).


回答5:

function countDays($y, $m, $ignore = false) 
{
    $result = 0;
    $loop = strtotime("$y-$m-01");
    do if(!$ignore or !in_array(strftime("%u",$loop),$ignore))
        $result++;
    while(strftime("%m",$loop = strtotime("+1 day",$loop))==$m);
    return $result;
}
echo countDays(2013,1,array(6,7));//23
echo countDays(2013,2);//28