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

查看:313
本文介绍了使用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_atCarbon实例的deleted_at列,该实例提供了 各种有用的方法,并扩展了本机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');

有没有人使用过这种碳包装,可以帮我做我需要的事情,我很困惑.

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 \Carbon\Carbon::createFromTimeStamp(strtotime($comment->created_at))->diffForHumans() ?>

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

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