how to calculate time diffrence 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: php get day diff
$now = time();
$your_date = strtotime("2010-01-31");
$datediff = $now - $your_date;
echo round($datediff / (60 * 60 * 24));
Example 3: calculate time difference php
$time1 = new DateTime('09:00:59');
$time2 = new DateTime('09:01:00');
$interval = $time1->diff($time2);
echo $interval->format('%s second(s)');
Result: 1 second(s)