I can get the difference between two dates (DD/MM/YY format) with the following code;
$date1 = new DateTime("1986-04-27");
$today = new DateTime("now");
$interval = $date1->diff($today);
$year = $interval->format('%y');
$month = $interval->format('%m');
$day = $interval->format('%d');
It works perfect when calculating 1 year 2 months but when I'm trying to calculate 1 year 2 months 3 days, it doesn't give me the accurate result.
I would like to get the difference between two dates, using 360 days for a year, 30 days for a month, without calculating leap years..
For example; if a month has 31 days the system will calculate it as 30 days. If a year has 365 days, the system will calculate it as 1 year 5 days. (360 + 5)
How can I do that?
Thank you..
Here is the way to do it:
function diff360($date1, $date2) {
$date1 = new DateTime($date1);
$date2 = new DateTime($date2);
$diff = $date1->diff($date2);
$days = ($date2->format('d') + 30 - $date1->format('d')) % 30;
return array(
"y" => $diff->y,
"m" => $diff->m,
"d" => $days,
"totaldays" => $diff->y * 360 + $diff->m * 30 + $days
);
}
$result = diff360("2016-02-06", "2016-03-06");
var_export($result);
Output:
array (
'y' => 0,
'm' => 1,
'd' => 0,
'totaldays' => 30,
)