使用 Carbon 返回人类可读的日期时间差 [英] Using Carbon to return a human readable datetime difference

查看:21
本文介绍了使用 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_atupdated_atdeleted_at 列到 Carbon 的实例,它提供了一个各种有用的方法,并扩展了原生 PHP DateTime班级.

By default, Eloquent will convert the created_at, updated_at, and deleted_at columns to instances of Carbon, which provides an assortment of helpful methods, and extends the native PHP DateTime 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() ?>

这篇关于使用 Carbon 返回人类可读的日期时间差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆