本文實例講述了php獲取給定日期相差天數的方法。分享給大家供大家參考,具體如下:
方法一:
<?phpfunction count_days($a,$b){ $a_dt=getdate($a); $b_dt=getdate($b); $a_new=mktime(12,0,0,$a_dt['mon'],$a_dt['mday'],$a_dt['year']); $b_new=mktime(12,0,0,$b_dt['mon'],$b_dt['mday'],$b_dt['year']); return round(abs($a_new-$b_new)/86400);}//今天與2017年8月26日相差多少天$date1=strtotime(date("Y-m-d"));$date2=strtotime('2017-8-26');$result=count_days($date1,$date2);echo $result;?>運行結果:187
方法二:
<?php//今天與2017年8月26日相差多少天$Date_1=date("Y-m-d");$Date_2="2017-8-26";$d1=strtotime($Date_1);$d2=strtotime($Date_2);$Days=round(($d2-$d1)/3600/24);echo "今天與2017年8月26日相差".$Days."天";?>運行結果:
今天與2017年8月26日相差187天
希望本文所述對大家PHP程序設計有所幫助。
新聞熱點
疑難解答
圖片精選