PHP 計算兩個時間戳之間相差的時間


假設你兩個時間戳為$a,$b;
你可以用$c=$a-$b;(反正就是大的減小的),這時$c就是兩個時間間隔的秒數了。

 

想求兩個時間間隔的天數就用:$c/(60*60*24)

一天的毫秒數是:86400,所以直接$c/86400 答案也是一樣的


想求兩個時間間隔的小時數就用:$c/(60*60)
//PHP 計算兩個時間戳之間相差的時間
//功能:計算兩個時間戳之間相差的日時分秒
//$begin_time  開始時間戳
//$end_time 結束時間戳
public function timediff($begin_time,$end_time)
{
    if($begin_time < $end_time){
        $starttime = $begin_time;
        $endtime = $end_time;
    }else{
        $starttime = $end_time;
        $endtime = $begin_time;
    }
    //計算天數
    $timediff = $endtime-$starttime;
    $days = intval($timediff/86400);
    //計算小時數
    $remain = $timediff%86400;
    $hours = intval($remain/3600);
    //計算分鍾數
    $remain = $remain%3600;
    $mins = intval($remain/60);
    //計算秒數
    $secs = $remain%60;
    $res = array("day" => $days,"hour" => $hours,"min" => $mins,"sec" => $secs);
    return $res;
}
public function ceshitime()
{
    return json($this->timediff(strtotime('2016-09-12 12:00:00'),strtotime('2016-09-15 21:50:21')));
     
}

  

結果

可實現 到期提醒  倒計時等功能

 

使用PHP實現計算兩個日期間隔的年、月、周、日數

<?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天直接返回天數  
            $result['daily'] = $extend;  
        }elseif($extend<=31){        //小於28天則返回周數,由於閏年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);  
                //判斷是否真的已經有了一年了,如果沒有的話就開減  
                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'));  
?>  

 

  

結果:

Array([extends]=>75[monthly]=>2[weekly]=>2)

php 查詢某天所在的周數及對應周的起始日期

/**  
* @file  
* @version  1.1  
* @author  Q  
* @date  2012-8-7 最后修改時間  
* @brief   
*/  
    //獲取某個日期的 周數、周對應的開始結束時間  
    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));  
    }  

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM