-2

I want to know what's the way to find the difference of two date. I get the startDate and endDate from a form. Now I want to find the difference of current date and startDate or 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");
  • 4
    Possible duplicate of [How to calculate the difference between two dates using PHP?](http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php) – currarpickt Jul 05 '16 at 03:18
  • That was 7 years ago, the version of php is too old. – Lucas Ting Jul 05 '16 at 03:23

1 Answers1

0

Try this.

$date1=strtotime("2016-07-02");
$date2=strtotime(date('Y/m/d'));
$diff = $date2 - $date1;
echo ($diff / (60 * 60 * 24)) . ' days';
noufalcep
  • 3,446
  • 15
  • 33
  • 51