dtxf759200 2016-09-15 19:45
浏览 110
已采纳

2时间戳之间的差异 - PHP

I am trying to calculate the difference between $timenow and $time.

$time = 2016-09-15 20:10:35
$timenow = 2016-09-15 20:40:42

I converted them to dateTime.

$time = new DateTime($time);
$timenow = new DateTime($timenow);

And then did the calculation:

$interval = $timenow->diff($time);
echo $interval;

Error: Catchable fatal error: Object of class DateInterval could not be converted to string

I looked at these difference between 2 timestamps in php and Wrong hour difference between 2 timestamps (hh:mm:ss)

Those didn't help.

  • 写回答

1条回答 默认 最新

  • douzhi1937 2016-09-15 19:51
    关注

    diff is going to return a DateInterval object full of good information about the difference between your two dates. You're just trying to echo that object which won't work. Do a var_dump() to see the object's properties:

    $time = "2016-09-15 20:10:35";
    $timenow = "2016-09-15 20:40:42";
    
    $time = new DateTime($time);
    $timenow = new DateTime($timenow);
    
    $interval = $timenow->diff($time);
    var_dump($interval);
    

    Then you can echo out the properties like:

    echo $interval->i; // minutes
    // 30
    

    http://php.net/manual/en/class.dateinterval.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求