Home > Article > Backend Development > Analysis of the method of obtaining the number of days difference between a given date in php
The example in this article describes the method of obtaining the number of days from a given date in php. Share it with everyone for your reference, the details are as follows:
Method 1:
<?php function 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; ?>
Running result: 187
Method 2:
<?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."天"; ?>
Running result:
今天与2017年8月26日相差187天
For more PHP methods to get the number of days difference between a given date and related articles, please pay attention to the PHP Chinese website!