Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php laravel-碳| diffForHumans()较短版本?_Php_Laravel_Date_Laravel 5.4_Php Carbon - Fatal编程技术网

Php laravel-碳| diffForHumans()较短版本?

Php laravel-碳| diffForHumans()较短版本?,php,laravel,date,laravel-5.4,php-carbon,Php,Laravel,Date,Laravel 5.4,Php Carbon,我试图找出如何缩短laravel中碳库提供的diffForHumans方法的输出 diffForHumans的默认格式如下:(来自文档) 将过去的值与现在的默认值进行比较时: 5个月前 一小时前 但我希望输出类似于: 1小时 5分钟 5个月 两年 刚才 如何实现这一点?根据diffForHumans /** * Get the difference in a human readable format in the current locale. * * * @param Carb

我试图找出如何缩短laravel中碳库提供的
diffForHumans
方法的输出

diffForHumans的默认格式如下:(来自文档)

  • 将过去的值与现在的默认值进行比较时:

  • 5个月前

  • 一小时前

但我希望输出类似于:

  • 1小时
  • 5分钟
  • 5个月
  • 两年
  • 刚才

  • 如何实现这一点?

    根据
    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
    

    碳元素让你可以选择去除“ago”

    $time=\Carbon\Carbon::now()->subMinutes(1)->diffForHumans(null,true)

    如果您需要使用“1小时5分钟”,只需
    str_替换(['hours'、'minutes']、['h'、'mins']、$time)

    对于
    刚才
    ,您需要指定
    刚才
    的长度