如何找到 PHP 中另一个日期范围内的日期范围内的天数?
How do I find how many days are within a date range that are within another date range in PHP?
我正在使用 Carbon PHP library.The 重复问题的答案使用 PHP 的内置函数。
count how many days within a date range are within another date range
下面是我用来查找日期范围($userDateStart
和 $userDateEnd
)是否在另一个日期范围($couponStart and
$couponEnd`)中的代码,它可以正常工作很好,没有任何错误,但我不知道如何找到 overlap/exists 在此日期范围内的日子?
我使用的库是http://carbon.nesbot.com/docs/
在这种情况下,预期结果应该是4..希望你能帮助我。
$userDateStart = Carbon::createFromFormat('Y-m-d','2015-06-26');
$userDateEnd = Carbon::createFromFormat('Y-m-d','2015-06-29');
$couponStart = Carbon::createFromFormat('Y-m-d','2015-06-26');
$couponEnd = Carbon::createFromFormat('Y-m-d','2015-10-31');
if(($userDateStart >= $couponStart && $userDateEnd <= $couponEnd) ||
($couponStart >= $userDateStart && $couponEnd <= $userDateEnd)){
die("Yes,The date is within this date range");
}
die("No,It is not within this date range");
根据提供的文档,您需要使用这个:
$dt = Carbon::create(2012, 4, 30, 0);
echo $dt->diffInDays($dt->copy()->addMonth()); // 30
echo $dt->diffInDays($dt->copy()->addWeek()); // 7
因此,要使用您的程序,我认为您需要这样做:
$userDateStart = Carbon::createFromFormat('Y-m-d','2015-06-26');
$userDateEnd = Carbon::createFromFormat('Y-m-d','2015-06-29');
$couponStart = Carbon::createFromFormat('Y-m-d','2015-06-26');
$couponEnd = Carbon::createFromFormat('Y-m-d','2015-10-31');
//Determin the highest date from the starts and the minimum dates from the ends
$startBetweenDate = $userDateStart->max($couponStart);
$endBetweenDate = $userDateEnd->min($couponEnd);
//Now find how many days are between
echo $startBetweenDate->diffInDays($endBetweenDate); //Should be 4
请注意:这没有经过测试,因为我没有安装 Carbon 的库。
我正在使用 Carbon PHP library.The 重复问题的答案使用 PHP 的内置函数。
count how many days within a date range are within another date range
下面是我用来查找日期范围($userDateStart
和 $userDateEnd
)是否在另一个日期范围($couponStart and
$couponEnd`)中的代码,它可以正常工作很好,没有任何错误,但我不知道如何找到 overlap/exists 在此日期范围内的日子?
我使用的库是http://carbon.nesbot.com/docs/
在这种情况下,预期结果应该是4..希望你能帮助我。
$userDateStart = Carbon::createFromFormat('Y-m-d','2015-06-26');
$userDateEnd = Carbon::createFromFormat('Y-m-d','2015-06-29');
$couponStart = Carbon::createFromFormat('Y-m-d','2015-06-26');
$couponEnd = Carbon::createFromFormat('Y-m-d','2015-10-31');
if(($userDateStart >= $couponStart && $userDateEnd <= $couponEnd) ||
($couponStart >= $userDateStart && $couponEnd <= $userDateEnd)){
die("Yes,The date is within this date range");
}
die("No,It is not within this date range");
根据提供的文档,您需要使用这个:
$dt = Carbon::create(2012, 4, 30, 0);
echo $dt->diffInDays($dt->copy()->addMonth()); // 30
echo $dt->diffInDays($dt->copy()->addWeek()); // 7
因此,要使用您的程序,我认为您需要这样做:
$userDateStart = Carbon::createFromFormat('Y-m-d','2015-06-26');
$userDateEnd = Carbon::createFromFormat('Y-m-d','2015-06-29');
$couponStart = Carbon::createFromFormat('Y-m-d','2015-06-26');
$couponEnd = Carbon::createFromFormat('Y-m-d','2015-10-31');
//Determin the highest date from the starts and the minimum dates from the ends
$startBetweenDate = $userDateStart->max($couponStart);
$endBetweenDate = $userDateEnd->min($couponEnd);
//Now find how many days are between
echo $startBetweenDate->diffInDays($endBetweenDate); //Should be 4
请注意:这没有经过测试,因为我没有安装 Carbon 的库。