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屋!