2016-07-05 163 views
-2

我想知道如何找到兩個日期的差異。我從表單中獲取startDate和endDate。現在我想查找當前日期和startDate或endDate的區別。在php中找到日期差異

echo $startDate = $startYear.'-'.$startMonth.'-'.$startDay.'<br>'; 
echo $endDate = $endYear.'-'.$endMonth.'-'.$endDay; 
$_SESSION['startDate'] = $startDate; 
$_SESSION['endDate'] = $endDate; 
$_SESSION['team'] = $team; 
header('location:test.php'); 

test.php的

date_default_timezone_set('Asia/Kuala_Lumpur'); 
    echo $startDate = $_SESSION['startDate']; 
    $date2=date_create(date('Y/m/d')); 
    $diff=date_diff($startDate,$date2); 
    echo $diff->format("%R%a days"); 
+4

的可能的複製[如何計算使用PHP的兩個日期之間的差異?](http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates -using-php) – currarpickt

+0

那是7年前的版本,php太舊了。 –

回答

0

試試這個。

$date1=strtotime("2016-07-02"); 
$date2=strtotime(date('Y/m/d')); 
$diff = $date2 - $date1; 
echo ($diff/(60 * 60 * 24)) . ' days';