首先,需要自己在后台创建一个表 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; }