How can we trim down diffForHumans()
?
Like $post->created_at->diffForHumans()
return the time ago Like 3 days ago
or 57 minutes ago
, or 2 hours ago
.
How can we be able to return 57 mins ago
or 1W ago
etc.
Is there any way around ?
Searched around but got nothing.
Carbon implements different call-time configurations through magic methods. The universe of possible configurations are documented in the backing trait. Scanning through those methods, it looks like you want shortRelativeDiffForHumans
:
$c = new Carbon\Carbon('now -1 day 4 hours');
dump($c->diffForHumans(), $c->shortRelativeDiffForHumans());
"20 hours ago"
"20h ago"
Failing that, you can use str_replace
or similar string functions to adjust the resulting value.
I'll note, in response to @Giovanni's contribution, that these magic methods are just verbose wrappers around the call to diffForHumans
. I prefer these longer method names and their variations, because they're self-documenting. Using diffForHumans
third argument of true
doesn't tell me much when scanning code a year later!