可能重复:
确定经过时间
我想知道如何计算数据库中两个日期之间的经过时间。
它们在日期/时间中分别保存为"EntryDate"answers"ExitDate"(DD-MM-YYYY 00:00:00),我想计算两者之间的时间,例如:
01.01.2012 12.00.00和01.01.2012 13.30.00,即1小时30分钟。
谢谢。类似
在mysql中工作时,您可能想要使用timediff。
mysql> SELECT TIMEDIFF('2000:01:01 00:00:00',
-> '2000:01:01 00:00:00.000001');
-> '-00:00:00.000001'
对于PHP中的实现,您需要使用date_diff函数。
$d_start = new DateTime($EntryDate);
$d_end = new DateTime($ExitDate);
$diff = $d_start->diff($d_end);
// return all data
$this->year = $diff->format('%y');
$this->month = $diff->format('%m');
$this->day = $diff->format('%d');
$this->hour = $diff->format('%h');
$this->min = $diff->format('%i');
$this->sec = $diff->format('%s');
使用此论坛线程:http://forums.devshed.com/php-development-5/calculate-remaining-day-minute-seconds-between-two-dates-538979.html
下一次谷歌发布前一点。我们会帮助你,但你不应该滥用。。。
因为日期的格式是这样的,所以可以使用strtotime
来获取每个日期的时间戳。然后你只需要从入口中减去出口。这就给你留下了以秒为单位的差异。现在你可以玩这个了。
您可以使用
eg:
select TIMEDIFF ('2006-10-31 11:50:31' , '2006-10-31 11:50:01')
SELECT TIMESTAMPDIFF(MINUTE,'2012-01-01 12:00:00','2012-01-01 13:30:00')
这将在几分钟内返回差额。您可以将分钟替换为任何时间单位(如"小时"、"秒"等)