I'm trying to figure out how to shorten the output from a diffForHumans
method provided by the Carbon library in laravel.
The default format of diffForHumans
is something like this:(from documentation)
When comparing a value in the past to default now:
5 months ago
1 hour ago
But I want the output to be something like:
How can I achieve this?
Luckily for us, Carbon is already included in Laravel.
The web application developed by the Laravel framework uses a simple API extension to work with the date and time called Carbon. This PHP package can handle the time and timezone more easily in the Laravel project.
As per the source code for diffForHumans
/**
* Get the difference in a human readable format in the current locale.
*
*
* @param Carbon|null $other
* @param bool $absolute removes time difference modifiers ago, after, etc
* @param bool $short displays short format of time units
*
* @return string
*/
public function diffForHumans(Carbon $other = null, $absolute = false, $short = false) {
...
}
to remove modifiers, pass the second parameter as true
and to get shortened version of time pass the third parameter as true
Source code at
vendor/nesbot/carbon/src/Carbon/Carbon.php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With