首頁  >  文章  >  後端開發  >  php日期方面

php日期方面

巴扎黑
巴扎黑原創
2016-11-29 11:52:071097瀏覽

date_default_timezone_set('PRC');
/**
* 求取從某日起經過一定天數後的日期,
* 排除週六週日及假日
* @param $start       開始日期
* @param $offset       開始日期
* @param $offset    * @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;
  ;
    //然後計算週六週日造成的偏移量
    $weekday = date('N', $starttime);//得到星期值:1-7
     2 * ($offset - $remain) / 7;//每一週需重新計算兩天
    if( $remain > 0 ){//週餘湊整
        tmp >= 7 ){
            $newoffset += 2;
        }else if( $        }
        //考慮使用於週六週日的情形
        if( $ weekday == 6 ){
            $newoffset -= 1;
        }else if(
        }
    }
    //再計算以假日所造成的偏移量
    if( is_array ($exception) ){//多節假日
        foreach ($exception as $day){
            $tmp_time = str       _time>$starttime && $tmp_time                $weekday_t = date('N', $tmp_time);
   
                    $newoffset += 1;
           
        }
    }else{//單一假日
        if( $exception!='' 
            if( $tmp_time>$starttime && $tmp_time         weekday_t = date('N', $tmp_time);
                if($weekday_t                 }
             }
    //依偏移天數,遞歸做等價運算
    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='' ){
    strtotime($start);
    $tmptime = $starttime + 24*3600;
    
    while( $offset > 0 ){
    while( $offset > 0 ){
   ).      $tmpday = date('Y-m - d', $tmptime);
        $bfd = false;//是否為假期
        if(is_array($except 
        }else{
            $bfd = ( $ exception==$tmpday);
        }
        if( $weekday";
        }
        $tmptime += 24*3600;
    }🠎p  
    '2010-01-01','2010- 01-02' ,' 2010-01-03',
    '2010-04-03','2010-04-04','2010-04-05',
    '2010-05-01','
    '2010-05-01','2
    '2010-05-01','2012' 2012' 2010-05-03',
    '2010-06-14','2010-06-15','2010-06-16',
    '2010-09-22','
    '2010-09-22','2
    '2010-09-22',2013' 2013' 2010-09-24',
    '2010-10-01','2010-10-02','2010-10-03','2010-10-04',
  ',0-155 2010-10-06','2010-10-07',
    
);
//echo getEndDay('2010-08-27',3,'');
//echo getEndDay('2010 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 "使用".($t3-$t2)." s
";    


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn