两次时间戳之间的时差-PHP [英] Diff between 2 timestamp - PHP

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

问题描述

我正在尝试计算$ timenow和$ time之间的时差。

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

我将它们转换为dateTime。

I converted them to dateTime.

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

然后进行计算:

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

错误:可捕获的致命错误:DateInterval类的对象无法转换为字符串

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

我看过这些
之间的差异php中的2个时间戳

错误两个时间戳之间的小时差(hh:mm:ss)

这些无济于事。

推荐答案

diff 将返回一个DateInterval对象,该对象包含有关两个日期之间的差的详细信息。您只是在尝试回显无效的对象。执行 var_dump()来查看对象的属性:

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

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

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