如何使用Laravel 5以人类可读的格式获取时差?

编程入门 行业动态 更新时间:2024-10-26 07:29:17
本文介绍了如何使用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_at和updated_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以人类可读的格式获取时差?

本文发布于:2023-11-11 06:33:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577642.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时差   如何使用   可读   人类   格式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!