您好我有一个数据库,其中包含访问者查看过的页面以及他们登录该页面的时间。我想要做的是花时间访问者查看页面A然后访问者查看页面b的时间,然后从B中减去A以获得访问者在该页面上停留的时间。
目前,在我的例子中,差异是两秒,但结果返回为零,这是因为2秒是一个低数字,结果被四舍五入?
这是我的代码。
$a = new DateTime('09:14:52');
$b = new DateTime('09:14:54');
$interval = $a->diff($b);
echo $interval->format("%H");
由于
答案 0 :(得分: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");
?>
答案 1 :(得分:0)
为什么不直接在日期使用strtotime?
echo strtotime('09:14:54') - strtotime('09:14:52') . ' Seconds';
这将输出
2 Seconds
答案 2 :(得分: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");