php时间戳之间的区别?或MySQL的时间戳?

问题描述:

我有两个时间戳使用php记录在mysql表上。如何使用php或mysql计算以小时为单位的这些时间戳之间的差异?php时间戳之间的区别?或MySQL的时间戳?

+0

你想看看这两个日期是不是天差更是什么? – 2010-08-25 19:08:09

如果实际小时(3600秒),这只是减去时间戳并除以3600的问题:

$hours = ($timestamp2 - $timestamp1)/3600; 
$hours = floor($hours); // to round down. 

或SQL中的类似问题。

如果你想在“逻辑小时”:

$tz = new DateTimezone("Europe/Lisbon"); //replace with actual timezone 
$d2 = new DateTime("@$timestamp2"); 
$d2->setTimezone($tz); 
$d1 = new DateTime("@$timestamp1"); 
$d1->setTimezone($tz); 
$hours = $d2->diff($d1)->h; 

这使得差异的情况下,出现了两个时间之间的DST的变化。例如:

<?php 
$ltz = new DateTimezone("Europe/Lisbon"); 
date_default_timezone_set("Europe/Lisbon"); 
$ts2 = strtotime("31-Oct-2010 02:30:00"); 
$ts1 = strtotime("31-Oct-2010 00:30:00"); 

$d2 = new DateTime("@$ts2"); 
$d2->setTimezone($ltz); 
$d1 = new DateTime("@$ts1"); 
$d1->setTimezone($ltz); 

var_dump(floor($ts2-$ts1)/3600); 
var_dump($d2->diff($d1)->h); 

给出:

 
float(3) 
int(2) 

在PHP中你可以使用常规的数学,然后使用日期()函数来创建以小时为单位表示的日期时间

完成在PHP中,看到这样的:http://php.about.com/od/advancedphp/qt/math_time_php.htm