Jhourlad Estrella
Jhourlad Estrella

Reputation: 3690

Laravel 5: Time Difference Between 2 Dates

Is there a way I could get a result like this:

Date1 is 1 minute 2 hours 3 days 4 years away from Date2

in Laravel 5 + Carbon between two dates?

Upvotes: 1

Views: 2185

Answers (1)

Jhourlad Estrella
Jhourlad Estrella

Reputation: 3690

It turned out Carbon::diffForHumans() method exists.

Some code for the curious:

$date1 = new Carbon();
$diff = $date1->diffForHumans($date2);
print 'Date1 is ' + $diff . ' away from Date2';

// output
// "Date1 is 3 weeks away from Date2"

Upvotes: 3

Related Questions