Date_default_timezone_set (' PRC ');
/**
* To obtain a date that takes a certain number of days from a date,
* Excluding Saturday Sundays and holidays
* @param $start Start date
* @param $offset after days
* @param $exception exceptions to the holiday
* @param $allow allowed dates (reservation parameters)
* @return
* Examples: Input (2010-06-25, 5, '), get 2010-07-02
*/
function Getendday ($start = ' Now ', $offset =0, $exception = ', $allow = ') {
The results of the Saturday Sundays and holidays are calculated first.
$starttime = Strtotime ($start);
$endtime = $starttime + $offset * 24 * 3600;
$end = Date (' y-m-d ', $endtime);
Then calculate the offsets caused by Sunday Saturday
$weekday = Date (' n ', $starttime);//Get Week value: 1-7
$remain = $offset% 7;
$newoffset = 2 * ($offset-$remain)/7;//two days to recalculate each week
if ($remain > 0) {//Zhou Yu rounding
$tmp = $weekday + $remain;
if ($tmp >= 7) {
$newoffset + 2;
}else if ($tmp = = 6) {
$newoffset + 1;
}
Consider the current situation for Saturday Sunday
if ($weekday = = 6) {
$newoffset-= 1;
}else if ($weekday = = 7) {
$newoffset-= 2;
}
}
Re-calculate the offset caused by the holiday
if (Is_array ($exception)) {//Multiple holidays
foreach ($exception as $day) {
$tmp _time = Strtotime ($day);
if ($tmp _time> $starttime && $tmp _time<= $endtime) {//within range (A,b]
$weekday _t = Date (' n ', $tmp _time);
if ($weekday _t <= 5) {//Prevent holidays and weekends from recurring
$newoffset + 1;
}
}
}
}else{//Individual Holidays
if ($exception!= ') {
$tmp _time = Strtotime ($exception);
if ($tmp _time> $starttime && $tmp _time<= $endtime) {
$weekday _t = Date (' n ', $tmp _time);
if ($weekday _t <= 5) {
$newoffset + 1;
}
}
}
}
Based on the number of offset days, recursive equivalence operation 111cn.net
if ($newoffset > 0) {
#echo ' [{$start}-> {$offset}] = [{$end}-> {$newoffset}] '. <br/> ";
Return Getendday ($end, $newoffset, $exception, $allow);
}else{
return $end;
}
}
/**
* Violence cycle approach
*/
function getendday2 ($start = ' Now ', $offset =0, $exception = ', $allow = ') {
$starttime = Strtotime ($start);
$tmptime = $starttime + 24*3600;
while ($offset > 0) {
$weekday = Date (' n ', $tmptime);
$tmpday = Date (' y-m-d ', $tmptime);
$BFD = whether false;//holidays
if (Is_array ($exception)) {
$BFD = In_array ($tmpday, $exception);
}else{
$BFD = ($exception = = $tmpday);
}
if ($weekday <=5 &&! $bfd) {//not weekends and holidays
$offset--;
#echo "tmpday={$tmpday}". <br/> ";
}
$tmptime + + 24*3600;
}
return $tmpday;
}
$exception = Array (
' 2010-01-01 ', ' 2010-01-02 ', ' 2010-01-03 ',
' 2010-04-03 ', ' 2010-04-04 ', ' 2010-04-05 ',
' 2010-05-01 ', ' 2010-05-02 ', ' 2010-05-03 ',
' 2010-06-14 ', ' 2010-06-15 ', ' 2010-06-16 ',
' 2010-09-22 ', ' 2010-09-23 ', ' 2010-09-24 ',
' 2010-10-01 ', ' 2010-10-02 ', ' 2010-10-03 ', ' 2010-10-04 ',
' 2010-10-05 ', ' 2010-10-06 ', ' 2010-10-07 ',
);
echo getendday (' 2010-08-27 ', 3, ');
echo getendday (' 2010-06-25 ', 15, ' 2010-07-07 ');
$t 1 = microtime ();
echo getendday (' 2010-05-12 ', A, $exception). " <br/> ";
$t 2 = Microtime (); echo "Use". ($t 2-$t 1). " s <br/> ";
echo getendday2 (' 2010-05-12 ', A, $exception). " <br/> ";
$t 3 = Microtime (); echo "Use". ($t 3-$t 2). " s <br/> ";