TALL Tales

Home About Resources Reference Posts Contact

Example of Using Carbon for your Dates

12 Jan, 2021



So, if you’re displaying something like posts or tasks on your site and you want to know how long ago they were created, then you’ll probably be pulling back the created_at date with something like this:

{{ $task->created_at }}

and it comes out on your page looking like this:

2019-09-11 10:26:21

If you’d prefer to show something more user-friendly then use Carbon (which is already in Laravel):

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

and instead you’ll get:

1 week ago

For more information about all the wonderful things you can do with Carbon, go here: Carbon.

Photo by Henry & Co. on Unsplash

Did this article help you? Say thanks by buying me a coffee!