calculate difference between two dates in months in php code example
Example 1: php calculate date difference
$d1 = new DateTime("2018-01-10 00:00:00");
$d2 = new DateTime("2019-05-18 01:23:45");
$interval = $d1->diff($d2);
$diffInSeconds = $interval->s;
$diffInMinutes = $interval->i;
$diffInHours = $interval->h;
$diffInDays = $interval->d;
$diffInMonths = $interval->m;
$diffInYears = $interval->y;
$d1 = strtotime("2018-01-10 00:00:00");
$d2 = strtotime("2019-05-18 01:23:45");
$totalSecondsDiff = abs($d1-$d2);
$totalMinutesDiff = $totalSecondsDiff/60;
$totalHoursDiff = $totalSecondsDiff/60/60;
$totalDaysDiff = $totalSecondsDiff/60/60/24;
$totalMonthsDiff = $totalSecondsDiff/60/60/24/30;
$totalYearsDiff = $totalSecondsDiff/60/60/24/365;
Example 2: how to calculate days difference between two dates in php
use DateTime;
$startDate = new DateTime($request->start_date);
$endDate = new DateTime($request->end_date);
$daysDifference = ($startDate->diff($endDate)->days);