hh:mm:ss 格式的两个日期之间的碳时间差异 [英] Carbon Difference in Time between two Dates in hh:mm:ss format
问题描述
我想弄清楚如何获取存储在我们数据库中的两个日期时间字符串并将其转换为 hh:mm:ss 的时间格式差异.
I'm trying to figure out how I can take two date time strings that are stored in our database and convert it to a difference in time format of hh:mm:ss.
我查看了 diffForHumans
,但这确实给出了我想要的格式并返回诸如 after
、ago
等;这很有用,但不适用于我正在尝试做的事情.
I looked at diffForHumans
, but that does give the format I'd like and returns things like after
, ago
, etc; which is useful, but not for what I'm trying to do.
持续时间不会跨越几天,最多只有几个小时.
The duration will never span days, only a max of a couple hours.
$startTime = Carbon::parse($this->start_time);
$finishTime = Carbon::parse($this->finish_time);
$totalDuration = $finishTime->diffForHumans($startTime);
dd($totalDuration);
// Have: "21 seconds after"
// Want: 00:00:21
推荐答案
我最终使用 Carbon 获取了总秒数差异:
I ended up grabbing the total seconds difference using Carbon:
$totalDuration = $finishTime->diffInSeconds($startTime);
// 21
然后使用gmdate
:
gmdate('H:i:s', $totalDuration);
// 00:00:21
如果有人有更好的方法,我会感兴趣.否则这行得通.
If anyone has a better way I'd be interested. Otherwise this works.
这篇关于hh:mm:ss 格式的两个日期之间的碳时间差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!