>  기사  >  php教程  >  php 排除周末与节假日程序实例代码

php 排除周末与节假日程序实例代码

WBOY
WBOY원래의
2016-05-25 16:42:301881검색

<?php
date_default_timezone_set(&#39;prc&#39;); 
/** 
* 求取从某日起经过一定天数后的日期, 
* 排除周六周日和节假日 
* @param $start       开始日期 
* @param $offset      经过天数 
* @param $exception 例外的节假日 
* @param $allow       允许的日期(预留参数) 
* @return//开源代码phprm.com 
*  examples:输入(2010-06-25,5,&#39;&#39;),得到2010-07-02 
*/ 
function getendday( $start=&#39;now&#39;, $offset=0, $exception=&#39;&#39;, $allow=&#39;&#39; ){ 
    //先计算不排除周六周日及节假日的结果 
    $starttime = strtotime($start); 
    $endtime = $starttime + $offset * 24 * 3600; 
    $end = date(&#39;y-m-d&#39;, $endtime); 
    //然后计算周六周日引起的偏移 
    $weekday = date(&#39;n&#39;, $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(&#39;n&#39;, $tmp_time); 
                if($weekday_t <= 5){//防止节假日与周末重复 
                    $newoffset += 1; 
                } 
            } 
        } 
    }else{//单个节假日 
        if( $exception!=&#39;&#39; ){ 
            $tmp_time = strtotime($exception); 
            if( $tmp_time>$starttime && $tmp_time<=$endtime ){ 
                $weekday_t = date(&#39;n&#39;, $tmp_time); 
                if($weekday_t <= 5){ 
                    $newoffset += 1; 
                } 
            } 
        } 
         
    } 
    //根据偏移天数,递归做等价运算111cn.net 
    if($newoffset > 0){ 
        #echo "[{$start} -> {$offset}] = [{$end} -> {$newoffset}]"."<br /> "; 
        return getendday($end,$newoffset,$exception,$allow); 
    }else{ 
        return $end; 
    } 
} 
/** 
* 暴力循环方法 
*/ 
function getendday2( $start=&#39;now&#39;, $offset=0, $exception=&#39;&#39;, $allow=&#39;&#39; ){ 
    $starttime = strtotime($start); 
    $tmptime = $starttime + 24*3600; 
     
    while( $offset > 0 ){ 
        $weekday = date(&#39;n&#39;, $tmptime); 
        $tmpday = date(&#39;y-m-d&#39;, $tmptime); 
        $bfd = false;//是否节假日 
        if(is_array($exception)){ 
            $bfd = in_array($tmpday,$exception); 
        }else{ 
            $bfd = ($exception==$tmpday); 
        } 
        if( $weekday<=5 && !$bfd){//不是周末和节假日 
            $offset--; 
            #echo "tmpday={$tmpday}"."<br />"; 
        } 
        $tmptime += 24*3600; 
    } 
     
    return $tmpday; 
} 
$exception = array( 
    &#39;2010-01-01&#39;,&#39;2010-01-02&#39;,&#39;2010-01-03&#39;, 
    &#39;2010-04-03&#39;,&#39;2010-04-04&#39;,&#39;2010-04-05&#39;, 
    &#39;2010-05-01&#39;,&#39;2010-05-02&#39;,&#39;2010-05-03&#39;, 
    &#39;2010-06-14&#39;,&#39;2010-06-15&#39;,&#39;2010-06-16&#39;, 
    &#39;2010-09-22&#39;,&#39;2010-09-23&#39;,&#39;2010-09-24&#39;, 
    &#39;2010-10-01&#39;,&#39;2010-10-02&#39;,&#39;2010-10-03&#39;,&#39;2010-10-04&#39;, 
    &#39;2010-10-05&#39;,&#39;2010-10-06&#39;,&#39;2010-10-07&#39;, 
     
); 
//echo getendday(&#39;2010-08-27&#39;,3,&#39;&#39;); 
//echo getendday(&#39;2010-06-25&#39;,15,&#39;2010-07-07&#39;); 
$t1 = microtime(); 
echo getendday(&#39;2010-05-12&#39;,66,$exception)."<br />"; 
$t2 = microtime();echo "use ".($t2-$t1)." s <br />"; 
echo getendday2(&#39;2010-05-12&#39;,66,$exception)."<br />"; 
$t3 = microtime();echo "use ".($t3-$t2)." s <br />";


永久链接:

转载随意!带上文章地址吧。

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.