• 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'));
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    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;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
  • 相关阅读:
    计算机视觉40例之案例14指纹识别
    如何解决多人编辑场景下的内容覆盖问题
    5分钟了解系统架构设计
    【Spring Security 实战 】Spring Security 整合 jwt 附源码
    【ESP32】Arduino C语言语法总结
    第10集丨龙场悟道:阳明心学的诞生
    wpa_supplicant介绍
    实现多方数据安全共享,解决普惠金融信息不对称难题
    大众CEO提前“毕业”,马斯克:软件是通向未来的关键
    SPI 机制详解
  • 原文地址:https://blog.csdn.net/qq_40641010/article/details/132805251