在PHP中减去两个日期 [英] Subtracting two dates in php
本文介绍了在PHP中减去两个日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在php中有两个约会
I have got two dates in php
$date1 = 'May 3, 2012 10:38:22 GMT'
$date2 = '06 Apr 2012 07:22:21 GMT'
然后我将它们都减去
$date2 - $date1
,然后获取
Result:6
为什么结果是6而不是27? ...?如何减去这两个日期,并使其减去月份& ;;天和时间?
Why is the result 6 and not 27? ... ? How can I subtract the two dates, and make it return me a result based on month differences while subtracting the years & days & time ?
推荐答案
第1部分:为什么结果为6?
Part 1: Why is the result 6?
当您初次减去日期时,它们只是字符串. PHP尝试将它们转换为整数.它通过转换直到第一个非数字来完成此操作.因此,date2变为6,date1变为0.
The dates are simply strings when you first subtract them. PHP attempts to convert them to integers. It does this by converting until the first non-number. So, date2 become 6 and date1 becomes 0.
第2部分:如何使用它?
Part 2: How do you get it to work?
$datetime1 = strtotime('May 3, 2012 10:38:22 GMT');
$datetime2 = strtotime('06 Apr 2012 07:22:21 GMT');
$secs = $datetime2 - $datetime1;// == <seconds between the two times>
$days = $secs / 86400;
进行适当的转换.
这篇关于在PHP中减去两个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文