在php中查找日期差异

时间:2016-07-05 03:16:26

标签: php

我想知道找到两个日期差异的方法。我从表单中获取startDate和endDate。现在我想找到当前日期与startDate或endDate的区别。

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");

1 个答案:

答案 0 :(得分:0)

试试这个。

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