如何减少两个日期在php [英] How to Minus two dates in php
问题描述
我想在php中减去两个日期
i want to minus two dates in php
例如:
$date1 = 08/16/2013;
$date2 = 08/23/2013;
$answer = date2 - date1;
$答案应该是7,我该怎么办?
非常感谢你
the $answer should be 7, How will i do that? thank you so much
推荐答案
开始使用 DateTime 日期/时间操纵类:
Start using DateTime class for date/time manipulation :
$date1 = new DateTime('08/16/2013');
$date2 = new DateTime('08/23/2013');
$diff = $date1->diff($date2);
print_r($diff); // or $diff->days
输出:
DateInterval Object
(
[y] => 0
[m] => 0
[d] => 7
[h] => 0
[i] => 0
[s] => 0
[invert] => 0
[days] => 7
)
阅读更多关于 DateTime:diff()。
请注意,各种 strtotime ()示例在日期/时间差计算中不正确。最简单的例子是 2013-03-31 21:00
和 2013-03-30 21:00
之间的区别。对于肉眼是肉眼确切1天的差异,但如果您减去此2个日期,您将得到 82800
秒,这是 0.95833333333333
天。这是因为从冬天到夏天的时间变化。 DateTime处理闰年和时区。
Please note that various strtotime() examples are not correct in date/time difference calculation. The simplest example is difference between 2013-03-31 21:00
and 2013-03-30 21:00
. Which for naked eye is exact 1 day difference, but if you do subtract this 2 dates, you will get 82800
seconds which is 0.95833333333333
days. This is because of the time change from winter to summer time. DateTime handles leap years and time-zones properly.
这篇关于如何减少两个日期在php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!