如何使用Laravel 5以人类可读的格式获取时差? [英] How do I get the difference of time in human readable format with Laravel 5?

查看:94
本文介绍了如何使用Laravel 5以人类可读的格式获取时差?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用存储在updated_at列中的日期和日期显示当前日期和时间之间的时差.但是,我希望它对人类友好,例如:

I want to display the difference between the current date and time with the one stored in the updated_at column. However, I want it to be human-friendly like:

53 mins ago
2 hours ago
3 days ago

有没有可以用来简化它的功能?

Is there a function out there that I could use to make it easier?

为确保您了解我,假设我的数据库中有一列(updated_at)等于2015-06-22 20:00:03,当前时间为20:00:28.然后我想看看:

To be sure that you understand me, let's say I have a column (updated_at) in my database which is equal to 2015-06-22 20:00:03 and the current time is 20:00:28. Then I'd like to see:

25 mins ago

当它高于59分钟时,我只想显示几小时,而当它高于24小时时,我想看看多少天前.

When it's higher than 59 minutes, I want to show only hours and when it's higher than 24 hours I'd like to see how many days ago.

推荐答案

默认情况下,Eloquent将created_atupdated_at列转换为.因此,如果您要使用Eloquent来获取数据,则可以按照以下步骤进行操作.

By default, Eloquent converts created_at and updated_at columns to instances of Carbon. So if you are fetching the data using Eloquent, then you can do it as below.

$object->updated_at->diffForHumans();

如果要自定义将自动突变的字段,则可以在模型中根据需要自定义它们.

If you want to customize the fields that will be mutated automatically, then within your model, you can customize them as you wish.

// Carbon instance fields
protected $dates = ['created_at', 'updated_at', 'deleted_at'];

这篇关于如何使用Laravel 5以人类可读的格式获取时差?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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