Nurdaulet Shamilov
Nurdaulet Shamilov

Reputation: 119

Displaying time in minutes/hours/days using Carbon or any other tools

I want to convert date to make it like, if i posted comment 2 minutes ago it will show 2 minutes ago, if 2 hours ago, like that, week ago, and so on

        <div class="col-md-2">
            <p class="text-secondary text-center">{{ $comment->created_at }}</p>
        </div>

enter image description here

Upvotes: 0

Views: 789

Answers (2)

Kishi
Kishi

Reputation: 201

You need to use diffForHumans()

$comment->created_at->diffForHumans();

https://carbon.nesbot.com/docs/#api-humandiff

Upvotes: 3

Tim Lewis
Tim Lewis

Reputation: 29257

You're looking for diffForHumans(), which is a Carbon method for returning a date relative to now. On Model instances, like your Comment class, the attribute created_at should already be converted to a Carbon instance, so you can simply call:

{{ $comment->created_at->diffForHumans() }}

This should return something similar to 1 hour ago, 5 months ago, etc. See https://carbon.nesbot.com/docs/#api-humandiff for full details.

Upvotes: 1

Related Questions