如何使用PHP计算剩余天数

时间:2017-03-09 07:28:08

标签: php

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

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

2 个答案:

答案 0 :(得分:2)

请参阅date_diff。试试这个:

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

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

答案 1 :(得分:0)

如果 date_diff 不起作用,您可以使用此功能。如果您想获取date1和date2之间的日期,或者您想要在两个给定日期之间的左侧天数,请使用此功能然后你可以增加一个像$ 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
    }

希望这能帮到你..