Laravel : Carbon shorten diffForHumans()
Asked Answered
L

4

5

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.

Leet answered 17/9, 2018 at 15:24 Comment(0)
U
6

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!

Usufruct answered 17/9, 2018 at 15:37 Comment(0)
H
7

The third value passed to diffForHumans() is for shortening the display output.

Try something like,

$post->created_at->diffForHumans(null, false, true)

here you can see a the comments for diffForHumans() and the values it accepts.


     /**
     * Get the difference in a human readable format in the current locale.
     *
     * When comparing a value in the past to default now:
     * 1 hour ago
     * 5 months ago
     *
     * When comparing a value in the future to default now:
     * 1 hour from now
     * 5 months from now
     *
     * When comparing a value in the past to another value:
     * 1 hour before
     * 5 months before
     *
     * When comparing a value in the future to another value:
     * 1 hour after
     * 5 months after
     *
     * @param Carbon|null $other
     * @param bool        $absolute removes time difference modifiers ago, after, etc
     * @param bool        $short    displays short format of time units
     * @param int         $parts    displays number of parts in the interval
     *
     * @return string
     */
    public function diffForHumans($other = null, $absolute = false, $short = false, $parts = 1)
    {
Helfant answered 17/9, 2018 at 15:40 Comment(0)
U
6

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!

Usufruct answered 17/9, 2018 at 15:37 Comment(0)
B
4

Try this.

shortRelativeDiffForHumans()

Docs

Bagworm answered 17/9, 2018 at 15:39 Comment(0)
O
-1

{{ $product->created_at->diffForHumans(null, false, 1) }}

Result: 14h ago, 14m ago, etc....

Odense answered 20/5, 2022 at 10:10 Comment(3)
How is this different from the top-voted answer?Philae
Slightly different syntax? Can you not see the difference?Odense
You mean using 1 instead of true? That just gets coerced to true. It has nothing to do with the question. In any case, when providing an answer that is just slightly different from existing ones (especially very highly rated answers) please take a moment to draw attention to the difference and explain how it is better. "Slightly different syntax" without any context does not justify a new answer.Philae

© 2022 - 2024 — McMap. All rights reserved.