猿问

将日期时间舍入到接下来的 15 分钟

在我的项目中,我需要将日期时间舍入,例如“08:10”到“08:00”。


更多例子


"8:20 到 8:15" "8:31 到 8:30"


在下一步中,我需要一个额外的方法来进行汇总。


例如“8:20 到 8:30”“8:31 到 8:45”


$shiftInPre = new \DateTime($row["time_start"] /* <-e.g. 8:02*/);

echo roundtoLastQuarterHour($shiftInPre);


慕妹3146593
浏览 177回答 2
2回答

jeck猫

如果您只有H:i而不是完整的日期时间,请使用基本的字符串操作$shiftInPre = '8:07';list($hours, $mins) = explode(':', $shiftInPre);$mins = $hours * 60 + $mins;$rounded = round($mins / 15) * 15;echo floor($rounded / 60) . ':' . str_pad($rounded % 60, 2, 0);&nbsp;// 8:07 >> 8:00// 8:08 >> 8:15// 8:18 >> 8:15
随时随地看视频慕课网APP
我要回答