计算时差 [英] Calculating Time Differences

查看:65
本文介绍了计算时差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在PHP中使用 DateTime ,并使用 diff 方法来查找时间戳和当前时间戳之间的时差时间。但是PHP给了我错误的区别。谁能向我指出我的代码出了什么问题?谢谢!

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代码

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天!

The difference is obviously 2 days, but I get 6015 days!

推荐答案

$ now-> diff($ now); ,应该是 $ now-> diff($ later)

这篇关于计算时差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆