Answers for "differnce between datetime and date php"

PHP
18

php calculate date difference

//get Date diff as intervals 
$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; //45
$diffInMinutes = $interval->i; //23
$diffInHours   = $interval->h; //8
$diffInDays    = $interval->d; //21
$diffInMonths  = $interval->m; //4
$diffInYears   = $interval->y; //1

//or get Date difference as total difference
$d1 = strtotime("2018-01-10 00:00:00");
$d2 = strtotime("2019-05-18 01:23:45");
$totalSecondsDiff = abs($d1-$d2); //42600225
$totalMinutesDiff = $totalSecondsDiff/60; //710003.75
$totalHoursDiff   = $totalSecondsDiff/60/60;//11833.39
$totalDaysDiff    = $totalSecondsDiff/60/60/24; //493.05
$totalMonthsDiff  = $totalSecondsDiff/60/60/24/30; //16.43
$totalYearsDiff   = $totalSecondsDiff/60/60/24/365; //1.35
Posted by: Guest on November-01-2019
0

Use DateTime() and DateInterval() Objects for PHP 5.3 and Above and Calculate the Difference Between Two Dates Using PHP

phpCopy$firstDate  = new DateTime("2019-01-01");
$secondDate = new DateTime("2020-03-04");
$intvl = $firstDate->diff($secondDate);

echo $intvl->y . " year, " . $intvl->m." months and ".$intvl->d." day"; 
echo "\n";
// Total amount of days
echo $intvl->days . " days ";

//output: 1 year, 2 months and 1 day
//        428 days
Posted by: Guest on April-23-2021

Code answers related to "differnce between datetime and date php"

Browse Popular Code Answers by Language