php獲取一段時間內的法定工作日

首先,需要自己在後臺創建一個表 content_cooper
這個結構如下:

CREATE TABLE `my_content_cooper` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '節假日記錄表id',
  `year` tinyint(3) NOT NULL DEFAULT '1' COMMENT '年份',
  `name` tinyint(3) NOT NULL DEFAULT '1' COMMENT '節假日名稱',
  `start_date` varchar(10) NOT NULL DEFAULT '2020-01-01' COMMENT '開始日期',
  `day_num` int(3) NOT NULL DEFAULT '3' COMMENT '放假時長(天數)',
  `need_date` varchar(255) DEFAULT NULL COMMENT '補班調休日期集合',
  `addtime` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `status` tinyint(3) DEFAULT '1',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of my_content_cooper
-- ----------------------------
INSERT INTO `my_content_cooper` VALUES ('1', '1', '7', '2019-10-01', '7', '[\"2019-09-28\",\"2019-10-12\"]', '2019-10-21 22:12:23', '1');
INSERT INTO `my_content_cooper` VALUES ('2', '2', '1', '2020-01-01', '1', '[\"\"]', '2019-10-21 22:15:59', '1');

創建表並寫入了兩條數據,

//計算法定節假日的日期和補班的日期
	function get_legal_days($start_date, $end_date){
		//只能先根據開始時間 爲基礎查詢出所有滿足的法定節假日
		//然後在所有查詢出來的結果中篩選沒有超過結束日期的所有列表
		$start_date = date('Y-m-d',strtotime($start_date));
		$end_date = date('Y-m-d',strtotime($end_date));
		$where=[];
		$where['status'] = 1;
		$where['start_date'] = array('egt',$start_date);
		$content_cooper = db('content_cooper')->where($where)->field('id,start_date,day_num,need_date')->select();
		$holiday = [];//節假日日期列表
		$weekday = [];//上班日期
		foreach($content_cooper as $k=>$v){
			if($v['start_date'] <= $end_date){
				$holiday[]=$v['start_date'];
			}
			$now_start_date = $v['start_date'];
			//循環放假天數
			if($v['day_num'] > 1){
				for($i=1;$i<$v['day_num'];$i++){
					$now_date = strtotime($now_start_date) + 86400 * $i;
					$days_date= date('Y-m-d',$now_date);
					if($days_date <= $end_date){
						//滿足在結束時間之內的日期
						$holiday[]=$days_date;
					}
				}
			}
			//計算補班的日期列表
			$need_date = json_decode($v['need_date'],true);
			foreach($need_date as $k1=>$v1){
				if( ($v1 >= $start_date) && ($v1 <= $end_date) ){
					$weekday[]=$v1;
				}
			}
		}
		return $holiday;//節假日
	}
	function get_legal_week_days($start_date, $end_date){
		//只能先根據開始時間 爲基礎查詢出所有滿足的法定節假日
		//然後在所有查詢出來的結果中篩選沒有超過結束日期的所有列表
		$start_date = date('Y-m-d',strtotime($start_date));
		$end_date = date('Y-m-d',strtotime($end_date));
		$where=[];
		$where['status'] = 1;
		$where['start_date'] = array('egt',$start_date);
		$content_cooper = db('content_cooper')->where($where)->field('id,start_date,day_num,need_date')->select();
		$weekday = [];//上班日期
		foreach($content_cooper as $k=>$v){
			//計算補班的日期列表
			$need_date = json_decode($v['need_date'],true);
			foreach($need_date as $k1=>$v1){
				if( ($v1 >= $start_date) && ($v1 <= $end_date) ){
					$weekday[]=$v1;
				}
			}
		}
		return $weekday;//上班列表
	}

/* 計算一段時間內的除去節假日的工作日列表 */
	function get_work_days($start_date, $end_date) {
		
		$data = array();
		if (strtotime($start_date) > strtotime($end_date)) list($start_date, $end_date) = array($end_date, $start_date);
		//先計算出這段時間內的天數
		$days = round(abs(strtotime($end_date) - strtotime($start_date))/86400) + 1;
		$data=[];//年月日格式 2009-10-16
		$start_time=strtotime($start_date);
		$week_date_arr = get_weekend_days($start_date, $end_date);//週末的日期
		$holiday_date_arr = get_legal_days($start_date, $end_date);//法定節假日的日期
		$holiday_arr = (array_values(array_unique( array_merge($week_date_arr , $holiday_date_arr) ) ) );//所有的法定節假日和週末的日期集合
		sort($holiday_arr);
		$start_date_old = $start_date;
		for($i=0;$i<$days;$i++){
			$start_date=date('Y-m-d',$start_time);
			if(!in_array($start_date,$holiday_arr)){
				$data[]=$start_date;
			}
			$start_time += 86400;
		}
		
		//在加上需要補的法定節假日的調休
		$get_legal_days = get_legal_week_days($start_date_old, $end_date);//法定節假日的調休日期
		$data = array_values ( array_unique( array_merge($data , $get_legal_days) ) );
		sort($data);
		
		return $data;
	}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章