<?php
class
work_days
{
function
get_week(
$date
)
{
$date_str
=
date
(
'Y-m-d'
,
strtotime
(
$date
));
$arr
=
explode
(
"-"
,
$date_str
);
$year
=
$arr
[0];
$month
= sprintf(
'%02d'
,
$arr
[1]);
$day
= sprintf(
'%02d'
,
$arr
[2]);
$hour
=
$minute
=
$second
= 0;
$strap
=
mktime
(
$hour
,
$minute
,
$second
,
$month
,
$day
,
$year
);
$number_wk
=
date
(
"w"
,
$strap
);
return
$number_wk
;
}
function
getDateFromRange(
$startdate
,
$enddate
)
{
$stimestamp
=
strtotime
(
$startdate
);
$etimestamp
=
strtotime
(
$enddate
);
$days
= (
$etimestamp
-
$stimestamp
) / 86400 + 1;
$_list_date
=
array
();
for
(
$i
= 0;
$i
<
$days
;
$i
++) {
$_list_date
[] =
date
(
'Y-m-d'
,
$stimestamp
+ (86400 *
$i
));
}
return
$_list_date
;
}
function
curl_post(
$url
,
$data
= null)
{
$curl
= curl_init();
curl_setopt(
$curl
, CURLOPT_URL,
$url
);
curl_setopt(
$curl
, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt(
$curl
, CURLOPT_SSL_VERIFYHOST, FALSE);
if
(!
empty
(
$data
)) {
curl_setopt(
$curl
, CURLOPT_POST, 1);
curl_setopt(
$curl
, CURLOPT_POSTFIELDS,
$data
);
}
curl_setopt(
$curl
, CURLOPT_RETURNTRANSFER, 1);
$output
= curl_exec(
$curl
);
curl_close(
$curl
);
return
$output
;
}
function
updateDate(
$year
)
{
$startDate
=
date
(
'Y-m-d'
,
strtotime
(
$year
.
'-01-01'
));
$endDate
=
date
(
'Y-m-d'
,
strtotime
(
'+1 year'
,
strtotime
(
$startDate
)) - 86400);
$_list_date
= self::getDateFromRange(
$startDate
,
$endDate
);
$url
=
'http://api.goseek.cn/Tools/holiday'
;//自行查找的免费API
$m
= M(
'tb_workday'
);
$count
= 0;
foreach
(
$_list_date
as
$k
=>
$_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
);
}
$_ret
&&
$count
++;
unset(
$_date
,
$_post_data
,
$_ret_curl
,
$_ret
);
}
return
$count
;
}
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;
}
}
}
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
;