How to calculate the difference between two dates in php?
The date_diff() is an inbuilt function in PHP which is used to calculate the difference between two dates.
1. Code:
<?php
function getDifferenceBetweenTwoDates($mySqlDate1, $mySqlDate2){
$date1 = date_create($mySqlDate1);
$date2 = date_create($mySqlDate2);
$diff = date_diff($date1, $date2);
return (int)$diff->format('%R%a');
}
$mySqlDate1 = "2018-11-15 00:00:00";
$mySqlDate2 = "2018-11-5 00:00:00";
echo(getDifferenceBetweenTwoDates($mySqlDate1, $mySqlDate2));
?>
2. Example:
Input:
"2018-11-15 00:00:00" "2018-11-5 00:00:00"
Output:
-10