PHP比较时间戳和实时时间戳的差异

PHP比较时间戳和实时时间戳的差异,php,datetime,Php,Datetime,我想比较两个时间戳和以60或10为基数的时间之间的差异。 更准确地说,如果时间戳1-2长于(或不长于)x小时y秒 我正在使用DateTime和DateInterval类,但是没有这样的函数,我也没有找到一个干净的解决方案 谢谢$timestam1=strottime('2012-01-26 14:00:00'); $timestamp2=STROTIME('2012-01-25 17:00:00'); 如果(abs($timestamp1-$timestamp2)getTimestamp()-$

我想比较两个时间戳和以60或10为基数的时间之间的差异。 更准确地说,如果时间戳1-2长于(或不长于)x小时y秒

我正在使用DateTime和DateInterval类,但是没有这样的函数,我也没有找到一个干净的解决方案

谢谢

$timestam1=strottime('2012-01-26 14:00:00');
$timestamp2=STROTIME('2012-01-25 17:00:00');
如果(abs($timestamp1-$timestamp2)<60*60*5/*(5小时)*/){
...

将时间戳和实时转换为UNIX时间戳。然后简单地减去以获得秒数差。

对于datetime对象:

$interval = $TempReceiptDateTime->diff($ShipDateTime);
echo $interval->format('%R%H:%I:%s days');

输出

11 hours 32 minutes 4 seconds

您有
timestamp1
timestamp2
数据的示例吗?$remainingtimebeforeignment=$TempReceiptDateTime->getTimestamp()-$ShipDateTime->getTimestamp()在
DateTime
DateInterval
中有这样的函数。有关使用DateTime和DateInterval的信息,请参见我的答案+1,这是正确确定小时数的唯一方法(同时也考虑了DST的变化)
$time1 = new DateTime("2011-01-26 01:13:30"); // string date
$time2 = new DateTime();
$time2->setTimestamp(1327560334); // timestamps,  it can be string date too.
$interval =  $time2->diff($time1);
echo $interval->format("%H hours %i minutes %s seconds");
11 hours 32 minutes 4 seconds