计算两个日期之间不含公众假期的工作日


Calculate Businessdays without public holidays between two dates

我有以下代码计算总工作日:(工作)

<?php
//get current month for example
if(!isset($_GET['from'])) {
    $beginday=date("Y-m-01");
} else {
    $beginday=date($_GET['from']);
}
if(!isset($_GET['to'])) {
    $lastday=date("Y-m-01");
} else {
    $lastday=date($_GET['to']);
}
$nr_work_days = getWorkingDays($beginday,$lastday);
echo $nr_work_days;
function getWorkingDays($startDate, $endDate){
 $begin=strtotime($startDate);
 $end=strtotime($endDate);
 if($begin>$end){
  echo "startdate is in the future! <br />";
  return 0;
 }else{
   $no_days=0;
   $weekends=0;
  while($begin<=$end){
    $no_days++; // no of days in the given interval
    $what_day=date("N",$begin);
     if($what_day>5) { // 6 and 7 are weekend days
          $weekends++;
     };
    $begin+=86400; // +1 day
  };
  $working_days=$no_days-$weekends;
  return $working_days;
 }
}
?>

我现在想念的是取消所有公共假期的功能。有什么建议吗?最好的方法是什么?

你可以使用

//Subtract the holidays
foreach($holidays as $holiday){
    $time_stamp=strtotime($holiday);
    //If the holiday doesn't fall in weekend
    if ($startDate <= $time_stamp && $time_stamp <= $endDate && date("N",$time_stamp) != 6 && date("N",$time_stamp) != 7)
        $working_days--;
}

注意: $holidays是您所有假期日期的数组。

参考:计算工作日