简体   繁体   English

Laravel:碳缩短 diffForHumans()

[英]Laravel : Carbon shorten diffForHumans()

How can we trim down diffForHumans() ?我们如何修剪diffForHumans()

Like $post->created_at->diffForHumans() return the time ago Like 3 days ago or 57 minutes ago , or 2 hours ago . Like $post->created_at->diffForHumans()返回时间之前 Like 3 days ago57 minutes ago2 hours ago

How can we be able to return 57 mins ago or 1W ago etc.我们怎么能在57 mins ago1W ago等返回。

Is there any way around ?有什么办法吗?

Searched around but got nothing.四处寻找,但一无所获。

The third value passed to diffForHumans() is for shortening the display output.传递给 diffForHumans() 的第三个值用于缩短显示输出。

Try something like,尝试类似的东西,

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

here you can see a the comments for diffForHumans() and the values it accepts.在这里,您可以看到 diffForHumans() 的注释及其接受的值。


     /**
     * 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)
    {

Carbon implements different call-time configurations through magic methods. Carbon 通过魔术方法实现不同的调用时间配置。 The universe of possible configurations are documented in the backing trait .可能配置的范围都记录在backing trait 中 Scanning through those methods, it looks like you want shortRelativeDiffForHumans :浏览这些方法,看起来你想要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.如果做不到这一点,您可以使用str_replace或类似的字符串函数来调整结果值。

I'll note, in response to @Giovanni's contribution , that these magic methods are just verbose wrappers around the call to diffForHumans .我会注意到,作为对@Giovanni贡献的回应,这些魔术方法只是对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!一年后扫描代码时,使用diffForHumans的第三个参数true并不能告诉我太多!

Try this.尝试这个。

shortRelativeDiffForHumans()

Docs文档

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

结果:14h 前、14m 前等......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM