我正在尝试减去当前日期时间与数据库中规定的时间之间的时间差。datetimes 当前格式是 yyyy:mm:dd hh:mm:ss,但是对于这种特定情况,我只想减去时间而不是日期,所以我只想计算 hh:mm:ss 然后存储到不同的我可以使用和格式化我想要的变量。是否可以获取完整的日期时间,将其分开并对其进行比较?我认为这有点令人困惑,所以请问您是否需要澄清。这是我迄今为止尝试过的:
<?php
//The time in the database
$classTime = DateTime::createFromFormat('Y-m-d H:i:s', '0000-00-00 18:30:00');
$timein = new DateTime("now", new DateTimeZone('America/Detroit'));
$timein->getTimestamp();
$timeout = $classTime;
$totaltime = $timeout->diff($timein);
$totaltime = $totaltime->format('Y-m-d H:i:s');
$totaltime = date('0000:00:00 H:i:s', strtotime($totaltime));
//I create a new date because i'm storing this time into the database, which can't be done with a datetime.
//FORMAT TIMES
$timein = $timein->format('Y-m-d H:i:s');
$timeout = $timeout->format('Y-m-d H:i:s');
echo "Time in " . $timein . " Time Out " . $timeout . " Total Time " . $totaltime;
?>
此电流输出返回:
Time in 2013-04-02 14:05:32 Time Out -0001-11-30 18:30:00 Total Time 0000:00:00 00:00:00
但我希望它返回如下内容:
Time in 2013-04-02 14:05:32 Time Out -0000-00-00 18:30:00 Total Time 0000:00:00 03:30:00