我想转换日期使它像,如果我发布评论2分钟前它将显示2分钟前,如果2小时前,像那样,一周前,等等
<div class="col-md-2">
<p class="text-secondary text-center">{{ $comment->created_at }}</p>
</div>
你需要使用diffForHumans()
$comment->created_at->diffForHumans();
你正在寻找diffForHumans()
,这是一个相对于现在返回日期的Carbon
方法。在Model
实例上,就像你的Comment
类一样,属性created_at
应该已经转换为Carbon
实例,所以你可以简单地调用:
{{ $comment->created_at->diffForHumans() }}
这应该返回类似于1 hour ago
,5 months ago
等的东西。有关详细信息,请参阅https://carbon.nesbot.com/docs/#api-humandiff。