欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

探討:如何使用PHP實現(xiàn)計算兩個日期間隔的年、月、周、日數(shù)

 更新時間:2013年06月13日 15:14:48   作者:  
本篇文章是對使用PHP實現(xiàn)計算兩個日期間隔的年、月、周、日數(shù)的方法進行了詳細的分析介紹,需要的朋友參考下

使用PHP實現(xiàn)計算兩個日期間隔的年、月、周、日數(shù):

復(fù)制代碼 代碼如下:

<?php
    function format($a,$b){
        //檢查兩個日期大小,默認前小后大,如果前大后小則交換位置以保證前小后大
        if(strtotime($a)>strtotime($b)) list($a,$b)=array($b,$a);
        $start  = strtotime($a);
        $stop   = strtotime($b);
        $extend = ($stop-$start)/86400;
        $result['extends'] = $extend;
        if($extend<7){                //如果小于7天直接返回天數(shù)
            $result['daily'] = $extend;
        }elseif($extend<=31){        //小于28天則返回周數(shù),由于閏年2月滿足了
            if($stop==strtotime($a.'+1 month')){
                $result['monthly'] = 1;
            }else{
                $w = floor($extend/7);
                $d = ($stop-strtotime($a.'+'.$w.' week'))/86400;
                $result['weekly']  = $w;
                $result['daily']   = $d;
            }
        }else{
            $y=    floor($extend/365);
            if($y>=1){                //如果超過一年
                $start = strtotime($a.'+'.$y.'year');
                $a     = date('Y-m-d',$start);
                //判斷是否真的已經(jīng)有了一年了,如果沒有的話就開減
                if($start>$stop){
                    $a = date('Y-m-d',strtotime($a.'-1 month'));
                    $m =11;
                    $y--;   
                }
                $extend = ($stop-strtotime($a))/86400;
            }
            if(isset($m)){
                $w = floor($extend/7);
                $d = $extend-$w*7;
            }else{
                $m = isset($m)?$m:round($extend/30);
                $stop>=strtotime($a.'+'.$m.'month')?$m:$m--;
                if($stop>=strtotime($a.'+'.$m.'month')){
                    $d=$w=($stop-strtotime($a.'+'.$m.'month'))/86400;
                    $w = floor($w/7);
                    $d = $d-$w*7;
                }
            }
            $result['yearly']  = $y;
            $result['monthly'] = $m;
            $result['weekly']  = $w;
            $result['daily']   = isset($d)?$d:null;
        }
        return array_filter($result);
    }

    print_r(format('2012-10-1','2012-12-15'));
?>

運行結(jié)果:
Array([extends]=>75[monthly]=>2[weekly]=>2)

php 查詢某天所在的周數(shù)及對應(yīng)周的起始日期

復(fù)制代碼 代碼如下:

/**
* @file
* @version  1.1
* @author  QQ83989686
* @date  2012-8-7 最后修改時間
* @brief
*/
    //獲取某個日期的 周數(shù)、周對應(yīng)的開始結(jié)束時間
    private function getWeekStartEndDay($day)
    {
        $g  = strftime("%u",strtotime($day));
        return array('week_num'=>strftime("%V",strtotime($day)),'week_start_day'=>strftime('%Y-%m-%d',strtotime($day)-($g-1)*86400),'week_start_day_cn'=>strftime('%Y年%m月%d日',strtotime($day)-($g-1)*86400),'week_end_day'=>strftime('%Y-%m-%d',strtotime($day) + (7-$g)*86400),'week_end_day_cn'=>strftime('%Y年%m月%d日',strtotime($day) + (7-$g)*86400));
    }

相關(guān)文章

最新評論