代码之家  ›  专栏  ›  技术社区  ›  Gammer

人类的碳排放()

  •  2
  • Gammer  · 技术社区  · 6 年前

    我们怎样才能减少开支 diffForHumans()

    就像 $post->created_at->diffForHumans() 3 days ago 57 minutes ago ,或 2 hours ago .

    57 mins ago 1W ago 等。

    有什么办法吗?

    3 回复  |  直到 6 年前
        1
  •  6
  •   bishop    6 年前

    Carbon通过神奇的方法实现了不同的调用时间配置。所有可能的配置都记录在 backing trait . 浏览这些方法,看起来你想要 shortRelativeDiffForHumans :

    $c = new Carbon\Carbon('now -1 day 4 hours');                                    
    dump($c->diffForHumans(), $c->shortRelativeDiffForHumans());                     
    
    "20 hours ago"
    "20h ago"
    

    str_replace 或类似的字符串函数来调整结果值。

    我会注意到,作为对 @Giovanni's contribution diffForHumans 人类的区别 true 一年后扫描代码的时候,我就没什么感觉了!

        2
  •  6
  •   Giovanni S    6 年前

    试试这样,

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

    在这里,您可以看到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)
        {
    
        3
  •  4
  •   zjbarg    6 年前

    试试这个。

    shortRelativeDiffForHumans()
    

    Docs