简体   繁体   English

包括两个日期之间的差额

[英]Get the difference between two dates inclusively

The following code gives me the correct answer 以下代码为我提供了正确的答案

//$start_date = '1/1/2013'
//$end_date = '1/7/2013'
$diff_num = strtotime($end_date) - strtotime($start_date) + 1; 
$diff_days = ceil($diff_num + 86400) / 86400;

However, it seems to me that there has got to be a better answer than this. 但是,在我看来,必须有一个比这更好的答案。 I don't like having to add a day to be completely inclusive of the range. 我不想增加一天以完全涵盖范围。 I want to include all the days in the range not the space in between. 我想将所有日期都包括在内,而不是介于两者之间。 Thanks for any help. 谢谢你的帮助。

You could use the DateTime object like so : 您可以像这样使用DateTime对象:

$datetime1 = new DateTime('2013-1-1');
$datetime2 = new DateTime('2013-7-1');
$interval = $datetime1->diff($datetime2);
$interval->add(new DateInterval('P1D')); // adds one day

But yes, you definitly have to add one day "by hand". 但是,是的,您必须手动添加一天。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM