思路:
1、获取到查询年份内所有工作日数据数组
2、获取到查询开始日期在工作日的索引
3、计算需查询日期索引
4、获得查询日期
/*创建日期类型记录表格*/ CREATE TABLE `tb_workday` ( `did` int(11) NOT NULL AUTO_INCREMENT, `exact_date` varchar(32) NOT NULL COMMENT '具体日期:格式date("Ymd");(20170205)', `date_year` varchar(32) NOT NULL COMMENT '具体日期:格式date("Y");(2017)', `date_type` tinyint(2) NOT NULL COMMENT '日期类型:0、工作日;1、特殊工作日;2、法定节假日', PRIMARY KEY (`did`) ) ENGINE=InnoDB AUTO_INCREMENT=829 DEFAULT CHARSET=utf8 COMMENT='各年工作日&法定节假日数据'
$_date) { $_ret = 0; $_date = date('Ymd', strtotime($_date)); $_post_data = array('date' => $_date); $_ret_curl = curl_post($url, $_post_data); $_ret_curl = json_decode($_ret_curl, true); //工作日 if ($_ret_curl['data'] == 0) { $dateData['exact_date'] = $_date; $dateData['date_year'] = $year; $dateData['date_type'] = 0; $_ret = $m->add($dateData) ? 1 : 0; unset($dateData); //工作日 判断是否为周末 if (in_array(self::get_week($_date), array(0, 1))) { //特殊工作日 $dateData['exact_date'] = $_date; $dateData['date_year'] = $year; $dateData['date_type'] = 1; $_ret = $m->add($dateData) ? 1 : 0; unset($dateData); } } //法定节假日 if ($_ret_curl['data'] == 2) { $dateData['exact_date'] = $_date; $dateData['date_year'] = $year; $dateData['date_type'] = 2; $_ret = $m->add($dateData) ? 1 : 0; unset($dateData); } //休息日(周末) 暂不处理 /*if ($_ret_curl['data'] == 1) { }*/ $_ret && $count++; unset($_date, $_post_data, $_ret_curl, $_ret); } return $count; } /** * 获取当年所有工作日 (从数据库获取,数据库无数据则先更新数据) * @param string $year 当年年份 * @return array */ private function getWorkDays($year) { $m = M('tb_workday'); $map['date_year'] = $year; $map['date_type'] = 0; $DateArray = $m->field('exact_date')->where($map)->select(); if (!empty($DateArray)) { $DateArray = array_column($DateArray, 'exact_date'); return $DateArray; } else { //更新数据库工作日数据 $ret = self::updateDate($year); if ($ret > 0) { return self::getWorkDays($year); } else { return false; } } } /** * 获取开始日期后第N个工作日具体日期 * @param $startdate string 计算开始日期 需包含年月日信息 * @param $days int 间隔天数 * @return mixed 成功返回 对应日期,失败返回false */ public function getNextWorkDate($startdate, $days) { $year = date('Y', strtotime($startdate)); $startdate = date('Y-m-d', strtotime($startdate)); $workDays = $this->getWorkDays($year); $search_key = array_search(date('Ymd', strtotime($startdate)), $workDays); if ($search_key === false) {//查询日期为非工作 //获取查询日期前最近工作日 $m = M('tb_workday'); $map['date_year'] = $year; $map['date_type'] = 0; $map['DATE_FORMAT(`exact_date`,'%Y-%m-%d')'] = array('LT', $startdate); $_search_date = $m->where($map)->order('`exact_date` DESC')->getField('exact_date'); $search_key = array_search($_search_date, $workDays); unset($m, $map, $_search_date); } $t_key = $search_key + $days; if ($t_key <= count($workDays) - 1) { return date('Y-m-d', strtotime($workDays[$t_key])); } else { //查询日期已跨年 $n_days = $days - (count($workDays) - 1 - $search_key); $next_year = $year + 1; return $this->getNextWorkDate($next_year . '-01-01', $n_days - 1); } } } $startdate = '2018-09-28'; $days = 5; $class = new work_days(); $_date_workday = $class->getNextWorkDate($startdate, $days); echo $_date_workday;//2018-10-10