2013-05-20 82 views
0

嗨,我有一个数据库,其中包含访问者浏览过的页面以及他们登录该页面的时间。我试图做的是花时间访问者浏览页面A,然后访问者浏览页面b的时间,然后从B中减去A,以了解访问者停留在该页面上的时间。从php减少时间

此刻,在我的例子中,差值是两秒,但结果返回为零,这是因为2秒是结果向上舍入的低位数?

这是我的代码。

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

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

感谢

+1

'的strtotime() - 的strtotime()'? –

+0

'%H'代表几个小时,所以0对我来说看起来像是预期的结果? http://www.php.net/manual/en/dateinterval.format.php –

回答

0
<?php 
$a = strtotime('09:14:52'); 
$b = strtotime('09:14:54'); 
$interval = $b-$a; 

echo $interval. " Seconds"; 
?> 

和你自己的代码是正确的也只是格式的标签是一个HOUR不是为SECOND。尝试

<?php 

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

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

?> 
0

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

echo strtotime('09:14:54') - strtotime('09:14:52') . ' Seconds'; 

这将输出

2 Seconds 
0
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");