我试图弄清楚如何缩短 laravel 中 Carbon 库提供的 diffForHumans 方法的输出。

diffForHumans 的默认格式是这样的:(来自文档)

  • 将过去的值与现在的默认值进行比较时:
  • 5 个月前
  • 1 小时前

  • 但我希望输出类似于:
  • 1 小时
  • 5 分钟
  • 5 个月
  • 2 年
  • 刚刚

  • 我怎样才能做到这一点?

    最佳答案

    根据 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) {
        ...
    }
    

    删除修饰符,将第二个参数作为 true 传递,为了缩短时间,将第三个参数作为 true 传递

    源代码在
    vendor/nesbot/carbon/src/Carbon/Carbon.php
    

    关于php - laravel - 碳 | diffForHumans() 更短的版本?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43732002/

    10-12 07:35