用php从时间中减去时间

用php从时间中减去时间,php,Php,嗨,我有一个数据库,其中有一个访问者浏览过的页面和他们在该页面上登陆的时间。我想做的是先计算访客浏览A页的时间,然后计算访客浏览b页的时间,然后从b中减去A,得到访客在该页上停留的时间 目前,在我的示例中,差值是2秒,但结果返回为零,这是因为2秒是一个较低的数字,结果被四舍五入吗 这是我的密码 $a = new DateTime('09:14:52'); $b = new DateTime('09:14:54'); $interval = $a->diff($b); echo $inte

嗨,我有一个数据库,其中有一个访问者浏览过的页面和他们在该页面上登陆的时间。我想做的是先计算访客浏览A页的时间,然后计算访客浏览b页的时间,然后从b中减去A,得到访客在该页上停留的时间

目前,在我的示例中,差值是2秒,但结果返回为零,这是因为2秒是一个较低的数字,结果被四舍五入吗

这是我的密码

$a = new DateTime('09:14:52');
$b = new DateTime('09:14:54');
$interval = $a->diff($b);

echo $interval->format("%H");

谢谢

为什么不在日期上直接使用strotime

echo  strtotime('09:14:54') - strtotime('09:14:52') . ' Seconds';
这将输出

2 Seconds

strotime()-strotime()
%H
代表小时,所以0看起来像我期望的结果?
echo  strtotime('09:14:54') - strtotime('09:14:52') . ' Seconds';
2 Seconds
date_default_timezone_set('America/Los_Angeles');// you should define a default time zone
$a = new DateTime('09:14:52');
$b = new DateTime('09:14:54');
$interval = $a->diff($b);

echo $interval->format("%s");