2

需求点

给定一个当前日期N,计算m个工作日后的天数x

话不多说,上代码

<?php
//节假日 需要手动维护配置文件或者放入db中
 $holiday=[
  '2017-04-29',
    '2017-04-30',

    '2017-05-01',
    '2017-05-06',
    '2017-05-07',
    '2017-05-13',
    '2017-05-14',
    '2017-05-20',
    '2017-05-21',
    '2017-05-28',
    '2017-05-29',
    '2017-05-30',

    '2017-06-03',
    '2017-06-04',
    '2017-06-10',
    '2017-06-11',
    '2017-06-17',
    '2017-06-18',
    '2017-06-24',
    '2017-06-25',

    '2017-07-01',
    '2017-07-02',
    '2017-07-08',
    '2017-07-09',
    '2017-07-15',
    '2017-07-16',
    '2017-07-22',
    '2017-07-23',
    '2017-07-29',
    '2017-07-30',


    '2017-08-05',
    '2017-08-06',
    '2017-08-12',
    '2017-08-13',
    '2017-08-19',
    '2017-08-20',
    '2017-08-26',
    '2017-08-27',

    '2017-09-02',
    '2017-09-03',
    '2017-09-09',
    '2017-09-10',
    '2017-09-16',
    '2017-09-17',
    '2017-09-23',
    '2017-09-24',

    '2017-10-01',
    '2017-10-02',
    '2017-10-03',
    '2017-10-04',
    '2017-10-05',
    '2017-10-06',
    '2017-10-07',
    '2017-10-08',
    '2017-10-14',
    '2017-10-15',
    '2017-10-21',
    '2017-10-22',
    '2017-10-28',
    '2017-10-29',

    '2017-11-04',
    '2017-11-05',
    '2017-11-11',
    '2017-11-12',
    '2017-11-18',
    '2017-11-19',
    '2017-11-25',
    '2017-11-26',

    '2017-12-02',
    '2017-12-03',
    '2017-12-09',
    '2017-12-10',
    '2017-12-16',
    '2017-12-17',
    '2017-12-23',
    '2017-12-24',
    '2017-12-30',
    '2017-12-31',

  //...
  ];

  function afterWorkDay($start_timestamp='',$add_workday_num='',$holiday=[]){

     // 如果加天数的的时候遇到休假日,则代表需要额外增加的天数
     $extra_day=0;

     //已经实际增加的天数的数量
     $workday_added_num=0;

     for($i=1;$i<=$add_workday_num;$i++){

          $detail_time=date('Y-m-d H:i:s',($start_timestamp)+$i*(60*60*24));

          $workday_added_num++;

          //只取宽泛的月日就好
          $wide_date=date('Y-m-d',strtotime($detail_time));


          // 如果其中一天在holiday中
          if(in_array($wide_date, $holiday)){
            //取消已经增加的天数,后面再继续加上
            $workday_added_num--;

           // 需要额外增加的天数
            $extra_day++;

           }
           //如果加完之后的日期的下一天还是休息日,需要再次增加额外增加天数
           $extra_judge=date("Y-m-d",strtotime($wide_date)+86400);

     }


     //如果需要增加的天数与实际已经增加的天数不相等
      if($add_workday_num!=$workday_added_num){

       while (in_array($extra_judge, $holiday)) {

          $extra_day++;

          $extra_judge=date("Y-m-d",strtotime($extra_judge)+86400);
      }
  }

  //实际应增加总天数=已经增加天数+(应增加天数-已经增加天数)+额外需要增加天数
   $add=$workday_added_num+($add_workday_num-$workday_added_num)+$extra_day;

   return date('Y-m-d H:i:s',($start_timestamp)+$add*(60*60*24));

    // $result_date=date('Y-m-d',($start_timestamp)+$add*(60*60*24));


    // 如果这个结果也是在假期日内
    // while (in_array($result_date, $holiday)) {
    //    $result_date=date('Y-m-d',strtotime($result) +86400);
    //    $result=date('Y-m-d H:i:s',strtotime($result) +86400);
    // }
 }

// xx发起时间 6月1日是周四,
 $time=mktime('13','30','56','06','01','2017');
 $start_timestamp=$time;
 // var_dump($start_timestamp);

//计算两个工作日后的时间
 echo afterWorkDay($start_timestamp,2,$holiday);//out put 2017-06-05 13:30:56

需要手动维护法定节假日及周末


牙小木木
1.5k 声望80 粉丝

iamtb.cn