In this article, we will learn how to calculate the difference between two dates and get the remaining days.


Using date_diff() function

<?php

$str_date1 = date_create('10-01-2021');
$str_date2 = date_create('24-01-2021');

//get the interval 
$intval = date_diff($str_date1, $str_date2);

echo $intval->format('Total Difference in Days is: %R%a days');
?>


Output:

Total Difference in Days is: +14 days


Custom PHP function

Custom PHP function to calculate date difference in day


index.php

<?php

//function to calculate date difference 
function dateDiff($date1, $date2) {
    $diff = strtotime($date2) - strtotime($date1);
    return abs(round($diff / 86400));
}

//call the function
$dateDiff = dateDiff("14-01-2021", "24-01-2021");
echo "difference in Days: " . $dateDiff. " days";
?> 

Output:

difference in Days: 10 days