gpt4 book ai didi

php - 碳 : displaying hours and minutes?

转载 作者:行者123 更新时间:2023-12-03 19:51:14 26 4
gpt4 key购买 nike

我最近一直在使用Carbon来显示人性化的时间字符串,但由于某种原因,我只能让它显示主要因素,例如,我有一个日期,它需要显示到该日期还有多长时间。例如,如果距离现在还有 6 天 4 小时 32 分钟,则当前仅显示“6 天”。

我将如何让它也显示小时数,可能还有分钟数?当它只给你几天的时候看起来有点可怕,很多人可能想知道更多的小时和秒?

我在 Carbon 文档中找不到任何关于此的内容。如果这甚至相关,我将在 laravel 5.3 View 中使用它。

这是我的代码:

{{ \Carbon\Carbon::createFromTimeStamp(strtotime($item->created_at))->diffForHumans() }}

最佳答案

你不应该使用 diffForHumans()在这种情况下,因为它只返回一个不能再处理的字符串。这是最终结果。

最好使用 Carbon对象,像这样:

$created = \Carbon\Carbon::createFromTimeStamp(strtotime($item->created_at));

然后,在 View 中添加:
{{ $created ->diff(\Carbon\Carbon::now())->format('%d days, %h hours and %i minutes'); }}

您可以延长更长的时间:
$created ->diff(\Carbon\Carbon::now())->format('%y year, %m months, %d days, %h hours and %i minutes');

编辑(根据评论):

如果你这样做:
$diff = $created->diff(Carbon::now());
var_dump($diff);

你得到这个结果:
object(DateInterval)[113]
public 'y' => int 0
public 'm' => int 0
public 'd' => int 6
public 'h' => int 4
public 'i' => int 32
public 's' => int 29
public 'f' => float 0.397424
public 'weekday' => int 0
public 'weekday_behavior' => int 0
public 'first_last_day_of' => int 0
public 'invert' => int 0
public 'days' => int 6
public 'special_type' => int 0
public 'special_amount' => int 0
public 'have_weekday_relative' => int 0
public 'have_special_relative' => int 0
// results depend of the current time

从那里,您浏览元素以创建满足您需求的最佳答案。

关于php - 碳 : displaying hours and minutes?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43952722/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com