2017-03-09 65 views
-1

我有这样的两个日期,如何计算日期之间使用php的剩余天数。如何计算使用PHP剩余天数

$date1='2016-12-26'; 
$date2='2017-03-21'; 
+1

有几十个类似的问题在左右。最初,从php.net手册中尝试自己,然后在遇到问题时查看此SO论坛。最后在你无法提问时提出问题。 – Thamilan

+1

'http:// php.net/manual/en/function.date-diff.php'这将有助于 – devpro

+0

您能告诉我们您尝试过什么吗? – Jerodev

回答

2

参考date_diff。尝试这样的:

$d1 = new DateTime($date1); 
$d2 = new DateTime($date2); 

$diff = $d2->diff($d1); 
echo $diff->days; // 85 
0

你可以使用这个如果date_diff,如果你想获得日期1和date2之间或日期不work.Use此功能如果你想在两个给定的日期之间留下剩余的日子,那么你可以增加一个像$ i这样的数字,并且可以返回$ i的值。

function date_range($date1, $date2, $step = '+1 day', $output_format = 'Y-m-d') { 
     $dates = array(); 
     $current = strtotime($first); 
     $last = strtotime($last); 
     $i=1; 
     while($current <= $last) { 
     $dates[] = date($output_format, $current); 
     $dateq = date($output_format, $current); 
     $current = strtotime($step, $current); 
     $i++; 
     }   
     //return $dates; if u want the dates 
     return $i; //if u want the count of dates between the tow given dates 
    } 

希望这有助于你..