计算经过的时间

时间:2012-02-27 13:33:10

标签: php mysql database datetime

  

可能重复:
  Determining elapsed time

我想知道如何计算数据库中两个日期之间的经过时间。

它们在日期/时间保存为'EntryDate'和'ExitDate'为DD-MM-YYYY 00:00:00,我想计算两者之间的时间,例如:

01.01.2012 12.00.00和01.01.2012 13.30.00这将是1小时30分钟。

感谢。山姆。

6 个答案:

答案 0 :(得分:2)

在mysql中工作时,你可能想要使用timediff

mysql> SELECT TIMEDIFF('2000:01:01 00:00:00',
->                 '2000:01:01 00:00:00.000001');
-> '-00:00:00.000001'

答案 1 :(得分:0)

对于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');

答案 2 :(得分:0)

使用此论坛主题:http://forums.devshed.com/php-development-5/calculate-remaining-day-minute-seconds-between-two-dates-538979.html

下次谷歌发布之前一点点。我们会帮助你,但你不应该滥用......

答案 3 :(得分:0)

由于日期格式如下,您可以使用strtotime获取每个日期的时间戳。然后你只需从条目中减去退出。这会留下几秒钟的差异。现在你可以玩这个了。

答案 4 :(得分:0)

你可以使用

eg:
select TIMEDIFF ('2006-10-31 11:50:31' , '2006-10-31 11:50:01')

答案 5 :(得分:0)

SELECT TIMESTAMPDIFF(MINUTE,'2012-01-01 12:00:00','2012-01-01 13:30:00')

这将以分钟为单位返回差异。您可以用任何时间单位替换分钟(如'HOUR','SECOND'等)

相关问题