PHP找到两个datetimes之间的区别 [英] PHP find difference between two datetimes

查看:165
本文介绍了PHP找到两个datetimes之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取两个数据时间之间的差异,并将其作为日期时间返回。我发现使用diff的例子,但我似乎无法正确的。

I'm trying to get the difference between two datetimes and return it as a datetime. I've found examples using diff but i can't seem to get it right.

$timein = date("Y-m-d H:i:s");
$timeout = date("Y-m-d 20:00:00");
$totaltime = $timein->diff($timeout);

然而,$总时间记录0000-00-00 00:00:00到我的数据库。这是因为我没有格式化我的总时间变量?

However $totaltime logs "0000-00-00 00:00:00" to my DB. Is this because i'm not formatting my totaltime variable?

推荐答案

我不知道你在寻找什么格式你的区别,但这里是如何使用DateTime

I'm not sure what format you're looking for in your difference but here's how to do it using DateTime

$datetime1 = new DateTime();
$datetime2 = new DateTime('2011-01-03 17:13:00');
$interval = $datetime1->diff($datetime2);
$elapsed = $interval->format('%y years %m months %a days %h hours %i minutes %S seconds');
echo $elapsed;

这篇关于PHP找到两个datetimes之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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