php 计算工作时间 排除节假日可设置补班

控制器内方法:

  //因法定节假日调休而上班的周末,这种情况也算工作日.因为这种情况少,可以通过手动配置
 $specialBusinessDay=[
      '2023-04-23',
      '2023-05-06',
      '2023-06-25',
      '2023-10-07',
      '2023-10-08',
  ];
  $calculator = new Dayscalculator(
      date('Y-m-d'), //当前时间
      Dayscalculator::getholidays(), // 获取节假日的时间
      [Dayscalculator::SATURDAY, Dayscalculator::SUNDAY],
      $specialBusinessDay
  );
  $calculator->addBusinessDays(2); // 2个工作日后的时间
  $afterBusinessDay = $calculator->getDate();
  // 开始时间
  $start_time = time();
  // 完结时间
  $should_time = strtotime($afterBusinessDay.date('H:i:s'));

Dayscalculator控制器:

<?php

namespace app\common\controller;

use app\admin\library\Auth;
use think\Config;
use think\Controller;
use think\Hook;
use think\Lang;
use think\Loader;
use think\Model;
use think\Session;
use fast\Tree;
use think\Validate;

/**
 * 后台控制器基类
 */
class Dayscalculator
{

    const MONDAY    = 1;
    const TUESDAY   = 2;
    const WEDNESDAY = 3;
    const THURSDAY  = 4;
    const FRIDAY    = 5;
    const SATURDAY  = 6;
    const SUNDAY    = 7;

    /**
     * @param DateTime   $startDate       Date to start calculations from
     * @param DateTime[] $holidays        Array of holidays, holidays are no conisdered business days.
     * @param DateTime[]      $nonBusinessDays Array of days of the week which are not business days.
     *  @param DateTime[]      $specialBusinessDay Array is the special work day.
     */
    public function __construct($startDate, $holidays = array(), $nonBusinessDays = array(), $specialBusinessDay = array()) {
        $this->date = $startDate;
        $this->holidays=[];
        foreach($holidays as $holiday){
            array_push($this->holidays,$holiday);
        }
        $this->nonBusinessDays = $nonBusinessDays;
        $this->specialBusinessDay = $specialBusinessDay;
    }

    public function addBusinessDays($howManyDays) {
        $i = 0;
        while ($i < $howManyDays) {
            $this->date = date("Y-m-d",strtotime("+1 day",strtotime($this->date)));
            if ($this->isBusinessDay($this->date)) {
                $i++;
            }
        }
    }

    public static function getholidays(){
        $date = [
            // 2023'2023-01-01',
            '2023-01-02',
            '2023-01-21',
            '2023-01-22',
            '2023-01-23',
            '2023-01-24',
            '2023-01-25',
            '2023-01-26',
            '2023-01-27',
            '2023-04-05',// 清明节
            '2023-04-29',// 劳动节
            '2023-04-30',
            '2023-05-01',
            '2023-05-02',
            '2023-05-03',
            '2023-06-22',// 端午节
            '2023-06-23',
            '2023-06-24',
            '2023-09-29',// 中秋节
            '2023-09-30', 
            '2023-10-01', 
            '2023-10-02', 
            '2023-10-03', 
            '2023-10-04', 
            '2023-10-05', 
            '2023-10-06', 
            '2023-10-06', 
        ];
        return $date;
    }

    public function getDate() {
        return $this->date;
    }

    private function isBusinessDay($date) {
        // var_dump($date);
        // var_dump($this->specialBusinessDay);die();
        if(in_array($date , $this->specialBusinessDay)){
          return true; //判断当前日期是否是因法定节假日调休而上班的周末,这种情况也算工作日  
        } 

        if (in_array(date('N',strtotime($date)), $this->nonBusinessDays)) {
            return false; //当前日期是周末
        }

        foreach ($this->holidays as $day) {
            if ($date == $day) {
                return false; //当前日期是法定节假日
            }
        }

        return true;
    }
}

你可能感兴趣的:(PHP,php,开发语言)