Carbon diffForHumans Laravel Example

WebSolutionStuff | Dec-14-2020 | Categories : Laravel

In this post i will show you Carbon diffForHumans Laravel Example, diffforhumans function in carbon provides human readable date and time formate in laravel or php.

When you want to display a date in the past with reference to the current time then you can use diffforhumans() carbon function, using diffforhumans carbon we can get result like this A few seconds ago, 30 minutes ago, 2 days ago, 1 year ago.

Example:

$post->created_at->diffForHumans() //output : 2 hours ago

 

When we need to compare value in the future to default now like, 30 minutes from now, 1 hour from now, 2 days from now.

$user->created_at->addDays(2)->diffForHumans() // output : 2 days from now

 

When we need to compare value in the past to another value like,30 minutes before, 1 hour before

$yesterday->diffForHumans($today) // output : 1 day before

 

When we need to compare value in the future to another value like,30 minutes after, 1 hour after, 1 day after

$tomorrow->diffForHumans($today) // output : 1 day after

 

Featured Post



Follow us
facebooklogo github instagram twitter