Search code examples
datetimelaravel-5php-carbon

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?


Solution

  • 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"