思路:
1、获取到查询年份内所有工作日数据数组
2、获取到查询开始日期在工作日的索引
3、计算需查询日期索引
4、获得查询日期
1 /*创建日期类型记录表格*/ 2 CREATE TABLE `tb_workday` ( 3 `did` int(11) NOT NULL AUTO_INCREMENT, 4 `exact_date` varchar(32) NOT NULL COMMENT '具体日期:格式date("Ymd");(20170205)', 5 `date_year` varchar(32) NOT NULL COMMENT '具体日期:格式date("Y");(2017)', 6 `date_type` tinyint(2) NOT NULL COMMENT '日期类型:0、工作日;1、特殊工作日;2、法定节假日', 7 PRIMARY KEY (`did`) 8 ) ENGINE=InnoDB AUTO_INCREMENT=829 DEFAULT CHARSET=utf8 COMMENT='各年工作日&法定节假日数据'
1 php 2 3 class work_days 4 { 5 /** 6 * 获取星期 7 * @param $date 8 * @return mixed 9 */ 10 function get_week($date) 11 { 12 //强制转换日期格式 13 $date_str = date('Y-m-d', strtotime($date)); 14 //封装成数组 15 $arr = explode("-", $date_str); 16 //参数赋值 17 //年 18 $year = $arr[0]; 19 //月,输出2位整型,不够2位右对齐 20 $month = sprintf('%02d', $arr[1]); 21 //日,输出2位整型,不够2位右对齐 22 $day = sprintf('%02d', $arr[2]); 23 //时分秒默认赋值为0; 24 $hour = $minute = $second = 0; 25 //转换成时间戳 26 $strap = mktime($hour, $minute, $second, $month, $day, $year); 27 //获取数字型星期几 28 $number_wk = date("w", $strap); 29 30 //获取数字对应的星期 31 return $number_wk; 32 33 //自定义星期数组 34 //$weekArr = array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"); 35 36 //获取数字对应的星期 37 //return $weekArr[$number_wk]; 38 } 39 40 41 /** 42 * 获取指定日期段内每一天的日期 43 * @param string $startdate 开始日期 44 * @param string $enddate 结束日期 45 * @return array 46 */ 47 function getDateFromRange($startdate, $enddate) 48 { 49 $stimestamp = strtotime($startdate); 50 $etimestamp = strtotime($enddate); 51 52 // 计算日期段内有多少天 53 $days = ($etimestamp - $stimestamp) / 86400 + 1; 54 55 // 保存每天日期 56 $_list_date = array(); 57 for ($i = 0; $i < $days; $i++) { 58 $_list_date[] = date('Y-m-d', $stimestamp + (86400 * $i)); 59 } 60 return $_list_date; 61 } 62 63 function curl_post($url, $data = null) 64 { 65 $curl = curl_init(); 66 curl_setopt($curl, CURLOPT_URL, $url); 67 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); 68 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); 69 if (!empty($data)) { 70 curl_setopt($curl, CURLOPT_POST, 1); 71 curl_setopt($curl, CURLOPT_POSTFIELDS, $data); 72 } 73 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 74 $output = curl_exec($curl); 75 curl_close($curl); 76 return $output; 77 } 78 79 /** 80 * 更新数据库指定年份日期数据 81 * @param $year 82 * @return int 83 */ 84 function updateDate($year) 85 { 86 $startDate = date('Y-m-d', strtotime($year . '-01-01')); 87 $endDate = date('Y-m-d', strtotime('+1 year', strtotime($startDate)) - 86400); 88 $_list_date = self::getDateFromRange($startDate, $endDate); 89 90 $url = 'http://api.goseek.cn/Tools/holiday';//自行查找的免费API 91 92 $m = M('tb_workday'); 93 $count = 0; 94 95 foreach ($_list_date as $k => $_date) { 96 $_ret = 0; 97 $_date = date('Ymd', strtotime($_date)); 98 $_post_data = array('date' => $_date); 99 $_ret_curl = curl_post($url, $_post_data); 100 $_ret_curl = json_decode($_ret_curl, true); 101 102 //工作日 103 if ($_ret_curl['data'] == 0) { 104 $dateData['exact_date'] = $_date; 105 $dateData['date_year'] = $year; 106 $dateData['date_type'] = 0; 107 $_ret = $m->add($dateData) ? 1 : 0; 108 unset($dateData); 109 110 111 //工作日 判断是否为周末 112 if (in_array(self::get_week($_date), array(0, 1))) { 113 //特殊工作日 114 $dateData['exact_date'] = $_date; 115 $dateData['date_year'] = $year; 116 $dateData['date_type'] = 1; 117 $_ret = $m->add($dateData) ? 1 : 0; 118 unset($dateData); 119 } 120 } 121 122 //法定节假日 123 if ($_ret_curl['data'] == 2) { 124 $dateData['exact_date'] = $_date; 125 $dateData['date_year'] = $year; 126 $dateData['date_type'] = 2; 127 $_ret = $m->add($dateData) ? 1 : 0; 128 unset($dateData); 129 } 130 131 //休息日(周末) 暂不处理 132 /*if ($_ret_curl['data'] == 1) { 133 134 }*/ 135 $_ret && $count++; 136 unset($_date, $_post_data, $_ret_curl, $_ret); 137 } 138 return $count; 139 } 140 141 /** 142 * 获取当年所有工作日 (从数据库获取,数据库无数据则先更新数据) 143 * @param string $year 当年年份 144 * @return array 145 */ 146 private function getWorkDays($year) 147 { 148 $m = M('tb_workday'); 149 $map['date_year'] = $year; 150 $map['date_type'] = 0; 151 $DateArray = $m->field('exact_date')->where($map)->select(); 152 if (!empty($DateArray)) { 153 $DateArray = array_column($DateArray, 'exact_date'); 154 return $DateArray; 155 } else { 156 //更新数据库工作日数据 157 $ret = self::updateDate($year); 158 if ($ret > 0) { 159 return self::getWorkDays($year); 160 } else { 161 return false; 162 } 163 } 164 } 165 166 /** 167 * 获取开始日期后第N个工作日具体日期 168 * @param $startdate string 计算开始日期 需包含年月日信息 169 * @param $days int 间隔天数 170 * @return mixed 成功返回 对应日期,失败返回false 171 */ 172 public function getNextWorkDate($startdate, $days) 173 { 174 $year = date('Y', strtotime($startdate)); 175 $startdate = date('Y-m-d', strtotime($startdate)); 176 177 $workDays = $this->getWorkDays($year); 178 179 $search_key = array_search(date('Ymd', strtotime($startdate)), $workDays); 180 181 if ($search_key === false) {//查询日期为非工作 182 //获取查询日期前最近工作日 183 $m = M('tb_workday'); 184 $map['date_year'] = $year; 185 $map['date_type'] = 0; 186 $map['DATE_FORMAT(`exact_date`,\'%Y-%m-%d\')'] = array('LT', $startdate); 187 $_search_date = $m->where($map)->order('`exact_date` DESC')->getField('exact_date'); 188 $search_key = array_search($_search_date, $workDays); 189 unset($m, $map, $_search_date); 190 } 191 192 $t_key = $search_key + $days; 193 194 if ($t_key <= count($workDays) - 1) { 195 return date('Y-m-d', strtotime($workDays[$t_key])); 196 } else { 197 //查询日期已跨年 198 $n_days = $days - (count($workDays) - 1 - $search_key); 199 $next_year = $year + 1; 200 return $this->getNextWorkDate($next_year . '-01-01', $n_days - 1); 201 } 202 } 203 } 204 205 206 $startdate = '2018-09-28'; 207 $days = 5; 208 209 $class = new work_days(); 210 $_date_workday = $class->getNextWorkDate($startdate, $days); 211 echo $_date_workday;//2018-10-10