计算时差
我尝试在 PHP 中使用 DateTime
并使用 diff
方法来查找时间戳与当前时间之间的时间差。然而 PHP 给了我错误的区别。谁能向我指出我的代码出了什么问题?谢谢!
PHP 代码
function time() {
$now = new DateTime;
$later = new DateTime('2011-10-17 07:08:00');
$interval = $now->diff($later);
echo $now->format('y m d');
echo "<br>";
echo $later->format('y m d');
echo "<br>";
echo $interval->format('%a');
}
输出
11 10 19
11 10 17
6015
明显相差 2 天,但我得到了 6015 天!
I tried using DateTime
in PHP and used diff
method to find the time difference between a timestamp and the current time. However PHP gives me the wrong difference. Can anyone point out to me what went wrong in my code? Thanks!
PHP Code
function time() {
$now = new DateTime;
$later = new DateTime('2011-10-17 07:08:00');
$interval = $now->diff($later);
echo $now->format('y m d');
echo "<br>";
echo $later->format('y m d');
echo "<br>";
echo $interval->format('%a');
}
Output
11 10 19
11 10 17
6015
The difference is obviously 2 days, but I get 6015 days!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在执行
$now->diff($now);
,应该是$now->diff($later)
。You are doing
$now->diff($now);
, should be$now->diff($later)
.正如所写,结果应该是 0,
因为你正在做 $now->diff($now)
如果你做 $later->diff($now) 你应该得到预期的结果。
as written, the result should be 0,
because you are doing $now->diff($now)
If you do $later->diff($now) you should get the expected result.