Date_default_timezone_set (' PRC ');
/**
* To find a date after a certain number of days from a certain date,
* Exclude Saturday Sundays and holidays
* @param $start Start date
* @param $offset days Elapsed
* @param $exception exceptions for holidays
* @param $allow allowed date (reserved 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 Weekday value: 1-7
$remain = $offset% 7;
$newoffset = 2 * ($offset-$remain)/7;//a recalculation of two days per week
if ($remain > 0) {//Spoke rounding
$tmp = $weekday + $remain;
if ($tmp >= 7) {
$newoffset + = 2;
}else if ($tmp = = 6) {
$newoffset + = 1;
}
Consider the current situation for the Saturday Sunday
if ($weekday = = 6) {
$newoffset-= 1;
}else if ($weekday = = 7) {
$newoffset-= 2;
}
}
To calculate the offsets caused by the holidays
if (Is_array ($exception)) {//Multiple holidays
foreach ($exception as $day) {
$tmp _time = Strtotime ($day);
if ($tmp _time> $starttime && $tmp _time<= $endtime) {//within the range (A, b]
$weekday _t = Date (' N ', $tmp _time);
if ($weekday _t <= 5) {//Prevent holidays and weekends from repeating
$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;
}
}
}
}
Recursive equivalent operation based on offset days
if ($newoffset > 0) {
#echo "[{$start}, {$offset}] = [{$end}, {$newoffset}]". " <br/>\n ";
Return Getendday ($end, $newoffset, $exception, $allow);
}else{
return $end;
}
}
/**
* Methods of violent circulation
*/
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 = false;//whether 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;
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service