使用 Carbon 返回人类可读的日期时间差
我正在使用 Laravel 4 创建我的项目.
I'm using Laravel 4 to create my project.
我目前正在构建评论部分,我想显示该帖子的创建时间,有点像 Facebook 的 '10 分钟前' &'2 周前' 等等.
I am currently building the comments section and I want to display how long ago the post was created, kind of like Facebook's '10 mins ago' & '2 weeks ago' etc.
我做了一点研究,发现一个包叫Carbon强>可以做到这一点.
I have done a little bit of research and found that a package called Carbon can do this.
阅读 Laravel 文档后,它说:
After reading the Laravel doc's, it says:
默认情况下,Eloquent 会转换 created_at
、updated_at
和deleted_at
列到 Carbon
的实例,它提供了一个各种有用的方法,并扩展了原生 PHP DateTime
班级.
By default, Eloquent will convert the
created_at
,updated_at
, anddeleted_at
columns to instances ofCarbon
, which provides an assortment of helpful methods, and extends the native PHPDateTime
class.
但是当我返回我创建的日期列时,它不会像在 Facebook 上那样显示.
But when I return a date column that I have created, it doesn't display it like on Facebook.
我使用的代码是:
return array('time');
有没有人使用过这个 Carbon 包来帮助我做我需要的事情,我很困惑.
Has any body used this Carbon package that could give me a hand in doing what I need, I'm quite confused.
推荐答案
如果您阅读 Carbon 文档以获取所需内容,请调用 diffForHumans()
方法.
If you read the Carbon docs to get what you want you call the diffForHumans()
method.
<?php echo CarbonCarbon::createFromTimeStamp(strtotime($comment->created_at))->diffForHumans() ?>
相关文章