dty47696 2014-07-29 08:05
浏览 27
已采纳

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

I have the following code calculating the total amount of business days: (working)

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

What i miss now is the feature that cuts out all the public holidays. Any suggestions whats the best way to perform this?

  • 写回答

1条回答 默认 最新

  • douqujin2767 2014-07-29 08:11
    关注

    You can use,

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

    Note: $holidays is an array of all of yours holidays date.

    Reference: Calculate business days

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助