Calculate the date difference between 2 dates in PHP

You can use the diff method  to calculate the dates difference between two DateTime object, for example:

$date1 = DateTime::createFromFormat('d-m-Y', '05-09-2017');
$now = new DateTime("now");
$diff = $now->diff($date1);
echo "Date difference: " . $diff->format('%a') ;

The result will be:

Date difference: 30

Comments