首页  >  文章  >  后端开发  >  php日期方面

php日期方面

巴扎黑
巴扎黑原创
2016-11-24 15:38:111955浏览

date_default_timezone_set('PRC');
/**
* 求取从某日起经过一定天数后的日期,
* 排除周六周日和节假日
* @param $start       开始日期
* @param $offset      经过天数
* @param $exception 例外的节假日
* @param $allow       允许的日期(预留参数)
* @return
*  examples:输入(2010-06-25,5,''),得到2010-07-02
*/
function getEndDay( $start='now', $offset=0, $exception='', $allow='' ){
    //先计算不排除周六周日及节假日的结果
    $starttime = strtotime($start);
    $endtime = $starttime + $offset * 24 * 3600;
    $end = date('Y-m-d', $endtime);
    //然后计算周六周日引起的偏移
    $weekday = date('N', $starttime);//得到星期值:1-7
    $remain = $offset % 7;
    $newoffset = 2 * ($offset - $remain) / 7;//每一周需重新计算两天
    if( $remain > 0 ){//周余凑整
        $tmp = $weekday + $remain;
        if( $tmp >= 7 ){
            $newoffset += 2;
        }else if( $tmp == 6 ){
            $newoffset += 1;
        }
        //考虑当前为周六周日的情况
        if( $weekday == 6 ){
            $newoffset -= 1;
        }else if( $weekday == 7 ){
            $newoffset -= 2;
        }
    }
    //再计算节假日引起的偏移
    if( is_array($exception) ){//多个节假日
        foreach ($exception as $day){
            $tmp_time = strtotime($day);
            if( $tmp_time>$starttime && $tmp_time<=$endtime ){//在范围(a,b]内
$weekday_t = date('N', $tmp_time);
if($weekday_t <= 5){//防止节假日与周末重复
$newoffset += 1;
}
}
}
}else{//单个节假日
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;
}
}
}

}
//根据偏移天数,递归做等价运算
if($newoffset > 0){
        #echo "[{$start} -> {$offset}] = [{$end} -> {$newoffset}]"."
\n";
        return getEndDay($end,$newoffset,$exception,$allow);
    }else{
        return $end;
    }
}
/**
* 暴力循环方法
*/
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;//是否节假日
        if(is_array($exception)){
            $bfd = in_array($tmpday,$exception);
        }else{
            $bfd = ($exception==$tmpday);
        }
        if( $weekday<=5 && !$bfd){//不是周末和节假日
            $offset--;
            #echo "tmpday={$tmpday}"."
";
        }
        $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');
$t1 = microtime();
echo getEndDay('2010-05-12',66,$exception)."
";
$t2 = microtime();echo "use ".($t2-$t1)." s
";
echo getEndDay2('2010-05-12',66,$exception)."
";
$t3 = microtime();echo "use ".($t3-$t2)." s
";

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn