使用 Carbon 或任何其他工具显示 minutes/hours/days 中的时间
Displaying time in minutes/hours/days using Carbon or any other tools
我想将日期转换成这样,如果我在 2 分钟前发表评论,它将显示 2 分钟前,如果是 2 小时前,就像那样,一周前,等等
<div class="col-md-2">
<p class="text-secondary text-center">{{ $comment->created_at }}</p>
</div>
您正在寻找 diffForHumans()
,这是一种 Carbon
方法,用于 return 计算相对于现在的日期。在 Model
实例上,例如 Comment
class,属性 created_at
应该已经转换为 Carbon
实例,因此您可以简单地调用:
{{ $comment->created_at->diffForHumans() }}
这应该 return 类似于 1 hour ago
、5 months ago
等。有关完整详细信息,请参阅 https://carbon.nesbot.com/docs/#api-humandiff。
您需要使用 diffForHumans()
$comment->created_at->diffForHumans();
我想将日期转换成这样,如果我在 2 分钟前发表评论,它将显示 2 分钟前,如果是 2 小时前,就像那样,一周前,等等
<div class="col-md-2">
<p class="text-secondary text-center">{{ $comment->created_at }}</p>
</div>
您正在寻找 diffForHumans()
,这是一种 Carbon
方法,用于 return 计算相对于现在的日期。在 Model
实例上,例如 Comment
class,属性 created_at
应该已经转换为 Carbon
实例,因此您可以简单地调用:
{{ $comment->created_at->diffForHumans() }}
这应该 return 类似于 1 hour ago
、5 months ago
等。有关完整详细信息,请参阅 https://carbon.nesbot.com/docs/#api-humandiff。
您需要使用 diffForHumans()
$comment->created_at->diffForHumans();