Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Armani's avatar
Level 17

Get date difference of 2 pairs of dates

I want to get the difference between 4 dates( 2 pairs of dates ) like this

$date1 = 1990-01-25;
$date2 = 1995-05-03;
$diff1 = date_diff($date1 , $date2);

$date3 = 2001-07-15;
$date4 = 2016-03-21;
$diff2 = date_diff($date3 , $date4);

$diff1 + $diff2;

I mean I want to get the exact years, months and days.

I know I can get the days of each pairs then calculate to get the years, months and days but is it the best way.

What is the best and most exact solution for this?

0 likes
15 replies
tykus's avatar

Well, since you cannot add two DateInterval instances, and even the y, m, d` properties on the instances are useless because you will potentially end up month/day overflowing, so the ball is in your court.

I would just get the difference in days, add the results, and convert to a human readable format from there using integer division and modulo

vincent15000's avatar

Have you tried with Carbon ?

$date1 = Carbon::createFromDate(year, month, day);
$diff1 = $date2 =Carbon::createFromDate(year, month, day); 
$diff2 = $date1->diffInDays($date2);

Tell me if it helps ;).

vincent15000's avatar

@tykus But it's possibile to convert the date interval in something else that can be added, no ?

tykus's avatar

@vincent15000 you can get days from the DateInterval instance. Carbon isn’t necessary

1 like
faxunil's avatar

Hi! what about Carbon? https://carbon.nesbot.com/docs/

echo $date->addRealHours(25)->format('H:i');                      // 02:00 (DST on)
echo $date->diffInRealHours('2014-03-30 00:00:00');               // 25
echo $date->diffInHours('2014-03-30 00:00:00');                   // 26
echo $date->diffInRealMinutes('2014-03-30 00:00:00');             // 1500
echo $date->diffInMinutes('2014-03-30 00:00:00');                 // 1560
echo $date->diffInRealSeconds('2014-03-30 00:00:00');             // 90000
echo $date->diffInSeconds('2014-03-30 00:00:00');                 // 93600
echo $date->diffInRealMilliseconds('2014-03-30 00:00:00');        // 90000000
echo $date->diffInMilliseconds('2014-03-30 00:00:00');            // 93600000
echo $date->diffInRealMicroseconds('2014-03-30 00:00:00');        // 90000000000
echo $date->diffInMicroseconds('2014-03-30 00:00:00');            // 93600000000
echo $date->subRealHours(25)->format('H:i');                      // 00:00 (DST off)

// with float diff:
$date = new Carbon('2019-10-27 00:00:00', 'Europe/Paris');       
echo $date->floatDiffInRealHours('2019-10-28 12:30:00');          // 37.5
echo $date->floatDiffInHours('2019-10-28 12:30:00');              // 36.5
echo $date->floatDiffInRealMinutes('2019-10-28 12:00:30');        // 2220.5
echo $date->floatDiffInMinutes('2019-10-28 12:00:30');            // 2160.5
echo $date->floatDiffInRealSeconds('2019-10-28 12:00:00.5');      // 133200.5
echo $date->floatDiffInSeconds('2019-10-28 12:00:00.5');          // 129600.5
// above day unit, "real" will affect the decimal part based on hours and smaller units
echo $date->floatDiffInRealDays('2019-10-28 12:30:00');           // 1.5625
echo $date->floatDiffInDays('2019-10-28 12:30:00');               // 1.5208333333333
echo $date->floatDiffInRealWeeks('2019-10-28 12:30:00');          // 0.22321428571429
echo $date->floatDiffInWeeks('2019-10-28 12:30:00');              // 0.2172619047619
echo $date->floatDiffInRealMonths('2019-10-28 12:30:00');         // 0.050403225806452
echo $date->floatDiffInMonths('2019-10-28 12:30:00');             // 0.049059139784946
echo $date->floatDiffInRealYears('2019-10-28 12:30:00');          // 0.0042808219178082
echo $date->floatDiffInYears('2019-10-28 12:30:00');              // 0.0041666666666667```
tykus's avatar

@faxunil did you read the bit about adding the two diff results????

tykus's avatar

@Tray2 it does not solve the issue of adding two DateInterval instances. Carbon's diff method returns DateInterval instances, just the same as date_diff; and there is a days property already available on DateInterval

Tray2's avatar

@tykus True but why do you need to compare four dates and get the difference?

It sounds to me like one of those pointless interview challenges.

A rough draft of something that needs more fine tuning depending on how many days of the months and leap years and such.

use \Carbon\Carbon;
$date1 = '1990-01-25';
$date2 = '1995-05-03';
$date3 = '2001-07-15';
$date4 = '2016-03-21';

$dt = new Carbon($date1);
$dt2 = new Carbon($date3);
$diffInYears = ($dt->diffInYears($date2) + $dt2->diffInYears($date4)) ;
$diffInMonths = ($dt->diffInMonths($date2) + $dt2->diffInMonths($date4)) - $diffInYears * 12;
$diffInDays = ($dt->diffInDays($date2) + $dt2->diffInDays($date4)) - $diffInYears * 365 - $diffInMonths * 30;

It gives 19 years, 11 months and 22 days.

1 like
Tray2's avatar

@tykus I know I just clicked the reply button of your reply instead of the OPs (pun intended).

Please or to participate in this conversation.