计算两个日期之间没有公共假期的营业日

时间:2014-07-29 08:05:23

标签: php

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

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

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

1 个答案:

答案 0 :(得分:1)

你可以使用,

//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是您所有假期日期的数组。

参考Calculate business days