42

I want to display the difference between the current date and time with the one stored in the updated_at column. However, I want it to be human-friendly like:

53 mins ago
2 hours ago
3 days ago

Is there a function out there that I could use to make it easier?

To be sure that you understand me, let's say I have a column (updated_at) in my database which is equal to 2015-06-22 20:00:03 and the current time is 20:00:28. Then I'd like to see:

25 mins ago

When it's higher than 59 minutes, I want to show only hours and when it's higher than 24 hours I'd like to see how many days ago.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Matthieu Boisjoli
  • 1,057
  • 2
  • 11
  • 17
  • Take a look at [this](https://gist.github.com/ozh/8169202) (found on google) – m_pro_m Jun 23 '15 at 00:34
  • Look at the DateTime class and the diff method [in the manual](http://php.net/manual/en/class.datetime.php) – RiggsFolly Jun 23 '15 at 00:34
  • 1
    You could use [Carbon](http://carbon.nesbot.com/), it comes with Laravel. – Stuart Wagner Jun 23 '15 at 00:34
  • 4
    huh...not at all, I was asking about a specific and existing function to get diff. of time IN laravel5. – Matthieu Boisjoli Jun 23 '15 at 01:03
  • 8
    [This question is being discussed on meta.](https://meta.stackoverflow.com/questions/376854/is-this-question-really-too-broad) – Script47 Nov 20 '18 at 16:04
  • 4
    Possible duplicate of [Calculate difference between two dates using Carbon and Blade](https://stackoverflow.com/q/39508963) and/or [Carbon Difference in Time between two Dates in hh:mm:ss format](https://stackoverflow.com/q/33575239) (taken from the duplicates listed prior to the question being reopened for the second time). – Makyen Nov 21 '18 at 05:06

3 Answers3

117

By default, Eloquent converts created_at and updated_at columns to instances of Carbon. So if you are fetching the data using Eloquent, then you can do it as below.

$object->updated_at->diffForHumans();

If you want to customize the fields that will be mutated automatically, then within your model, you can customize them as you wish.

// Carbon instance fields
protected $dates = ['created_at', 'updated_at', 'deleted_at'];
Burak
  • 5,252
  • 3
  • 22
  • 30
  • 1
    Adding to this answer, you do NOT need to add 'created_at' and 'updated_at' to the $dates array as they are automatically added. To override this and remove them, set $timestamps property to false – usernotnull Aug 21 '20 at 12:14
  • This solution is correct and still applicable to Laravel 10, thank you Burak – Two May 13 '23 at 02:27
10

incase of the date is in a string format, use laravel Carbon; e.g,

 {{ \Carbon\Carbon::parse($on_revision->assignment->deadline)->diffForhumans() }}

Notice how I wrap my string in the carbon::parse()

Wainaina Nik
  • 163
  • 1
  • 8
1
function timeDiff($firstTime, $lastTime): string
{
    $firstTime = strtotime($firstTime);
    $lastTime = strtotime($lastTime);

    $difference = $lastTime - $firstTime;

    $data['years'] = abs(floor($difference / 31536000));
    $data['days'] = abs(floor(($difference - ($data['years'] * 31536000)) / 86400));
    $data['hours'] = abs(floor(($difference - ($data['years'] * 31536000) - ($data['days'] * 86400)) / 3600));
    $data['minutes'] = abs(floor(($difference - ($data['years'] * 31536000) - ($data['days'] * 86400) - ($data['hours'] * 3600)) / 60));

    $timeString = '';

    if ($data['years'] > 0) {
        $timeString .= $data['years'] . " Years, ";
    }

    if ($data['days'] > 0) {
        $timeString .= $data['days'] . " Days, ";
    }

    if ($data['hours'] > 0) {
        $timeString .= $data['hours'] . " Hours, ";
    }

    if ($data['minutes'] > 0) {
        $timeString .= $data['minutes'] . " Minutes";
    }

    return $timeString;
}
Umair Tanveer
  • 11
  • 1
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 08 '21 at 08:13