2012-02-27 88 views
-1

可能重复:
Determining elapsed time计算经过时间

我想知道我怎么能计算出经过时间beween 2日我有一个DB。

它们被保存在日期“EntryDate”和“ExitDate” /时间DD-MM-YYYY 00:00:00,我想这两者之间来计算时间,例如:

01.01.2012 12.00 .00和01.01.2012 13.30.00这将是1小时30分钟。

谢谢。山姆。

+0

你想选择查询哪个给你时差? – Poonam 2012-02-27 13:36:10

回答

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'); 
0

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

2

在mysql内部工作时,您可能想使用timediff

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

可以使用

eg: 
select TIMEDIFF ('2006-10-31 11:50:31' , '2006-10-31 11:50:01') 
0
SELECT TIMESTAMPDIFF(MINUTE,'2012-01-01 12:00:00','2012-01-01 13:30:00') 

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