gpt4 book ai didi

php - 2 个日期范围之间的交叉点数

转载 作者:行者123 更新时间:2023-12-04 00:07:48 25 4
gpt4 key购买 nike

我的 Codeigniter 应用程序中有 2 个日期范围,我想计算这些范围之间相交的天数。有什么想法吗?

Date1start = YYYY-MM-DD;Date1end = YYYY-MM-DD;

Date2start = YYYY-MM-DD;Date2end = YYYY-MM-DD;

最佳答案

这样的东西应该可以工作

$datetimeStart1 = new DateTime('2015-12-10');
$datetimeEnd1 = new DateTime('2015-12-20');

$datetimeStart2 = new DateTime('2015-12-12');
$datetimeEnd2 = new DateTime('2015-12-28');

// following http://stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap
if ($datetimeStart1 < $datetimeEnd2 && $datetimeEnd1 > $datetimeStart2) {
echo min($datetimeEnd1,$datetimeEnd2)->diff(max($datetimeStart2,$datetimeStart1))->days+1;
} else {
echo 'no overlap';
}

演示:http://3v4l.org/9Pecb

仅适用于 PHP 5.2

$datetimeStart1 = new DateTime('2015-12-10');
$datetimeStart1 = $datetimeStart1->format('U');
$datetimeEnd1 = new DateTime('2015-12-20');
$datetimeEnd1 = $datetimeEnd1->format('U');

$datetimeStart2 = new DateTime('2015-12-12');
$datetimeStart2 = $datetimeStart2->format('U');
$datetimeEnd2 = new DateTime('2015-12-28');
$datetimeEnd2 = $datetimeEnd2->format('U');

// following http://stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap
if ($datetimeStart1 < $datetimeEnd2 && $datetimeEnd1 > $datetimeStart2) {
echo round(
((min($datetimeEnd1,$datetimeEnd2)) - (max($datetimeStart2,$datetimeStart1))) / (60*60*24)) + 1;
} else {
echo 'no overlap';
}

演示:http://3v4l.org/a1WLk

关于php - 2 个日期范围之间的交叉点数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30438999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com