Laravel : Carbon 缩短 diffForHumans()
Laravel : Carbon shorten diffForHumans()
怎样才能trim下来diffForHumans()
?
前段时间点赞$post->created_at->diffForHumans()
return 点赞3 days ago
or 57 minutes ago
, or 2 hours ago
.
我们怎样才能return57 mins ago
或1W ago
等等
有什么办法吗?
Searched around but got nothing.
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
或类似的字符串函数来调整结果值。
我会注意到,作为对 的回应,这些魔术方法只是对 diffForHumans
调用的冗长包装。我更喜欢这些较长的方法名称及其变体,因为它们是自我记录的。一年后扫描代码时,使用 diffForHumans
true
的第三个参数并没有告诉我太多信息!
试试这个。
shortRelativeDiffForHumans()
传递给 diffForHumans() 的第三个值用于缩短显示输出。
尝试类似
$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)
{
{{ $product->created_at->diffForHumans(null, false, 1) }}
结果: 14h 前, 14m 前, 等等....
怎样才能trim下来diffForHumans()
?
前段时间点赞$post->created_at->diffForHumans()
return 点赞3 days ago
or 57 minutes ago
, or 2 hours ago
.
我们怎样才能return57 mins ago
或1W ago
等等
有什么办法吗?
Searched around but got nothing.
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
或类似的字符串函数来调整结果值。
我会注意到,作为对 diffForHumans
调用的冗长包装。我更喜欢这些较长的方法名称及其变体,因为它们是自我记录的。一年后扫描代码时,使用 diffForHumans
true
的第三个参数并没有告诉我太多信息!
试试这个。
shortRelativeDiffForHumans()
传递给 diffForHumans() 的第三个值用于缩短显示输出。
尝试类似
$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)
{
{{ $product->created_at->diffForHumans(null, false, 1) }}
结果: 14h 前, 14m 前, 等等....