- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个在线成员(member)系统,本地执法部门要求我们为所有成员(member)提供 5 年的地址历史记录。在这 5 年内,允许有重叠,但不得有任何差距。成员输入他们的地址,存储在 mysql 数据库中,其中包含地址和每个地址的起始日期和截止日期。数据由成员(member)输入为月份和年份。这存储为“开始”日期的第一个月和“结束”日期的最后一个月。
我有我的 $array,它是从数据库的 mysqli 查询中编译的,用于为特定成员提取他们提供的地址的所有日期范围。这个数组很好地填充了我要制作的循环。
$array[] = array("from"=>$row['date_from'],"to"=>$row['date_to']);
我已经尝试将这些范围中的每一个转换为天数长度并将它们全部相加,但地址重叠的地方(允许和必要)它可能超过 5 年,即使地址历史记录中存在间隙也是如此。
我只需要 $complete 为 TRUE 或 FALSE,我不需要间隔天数。
愿意将其作为 mysql 查询或 php。
很抱歉没有提供半工作代码 - 我唯一的想法是循环 1825 天(5 年)以查看是否每一天都被代表。
最佳答案
此递归函数将遍历您的范围数组,组合重叠的范围,直到它们尽可能多地组合在一起。如果结果超过一个范围,则存在差距。我还使用了一个辅助函数来更清晰/更容易地查找重叠,并且我假设您的日期看起来像 'Y-m-d'
。
<?php
// First, here's how to use it. If we can't combine all the ranges, and the
// final combined range doesn't reach back 5 years or to the present,
// we have a gap.
$mergedRanges = combineRanges($myArray);
if (count($mergedRanges) > 1
|| $mergedRanges[0]['from'] > date('Y-m-d', time() - 157680000) // 5 years ago
|| $mergedRanges[0]['to'] < date('Y-m-d')) // present
echo 'Gaps found';
/**
* Recursive function to combine ranges.
*
* @param array $ranges
* @return array Array of combined ranges (has only 1 element if no gaps)
*/
function combineRanges(array $ranges)
{
$mergedRanges = array();
$usedKeys = array();
// Nested foreach compares each unique pair of ranges for overlap.
// If the a range has already been accounted for, it can be skipped.
foreach ($ranges as $k1 => $range1) {
if (!in_array($k1, $usedKeys)) {
foreach ($ranges as $k2 => $range2) {
if (!in_array($k1, $usedKeys) && $k1 > $k2) {
// If ranges overlap, combine them and make a note that
// they've already been included
if (rangesOverlap($range1, $range2)) {
$newRange = array(
'from' => min($range1['from'], $range2['from']),
'to' => max($range1['to'], $range2['to'])
);
// It's possible the resulting range could already
// be accounted for by a different combo of ranges,
// so check first
if (!in_array($newRange, $mergedRanges))
$mergedRanges[] = $newRange;
$usedKeys[] = $k1;
$usedKeys[] = $k2;
// Otherwise, add the 2nd range to $mergedRanges
} elseif (!in_array($k2, $usedKeys)) {
$mergedRanges[] = $range2;
$usedKeys[] = $k2;
}
// If $range1 didn't have any overlaps, add it here
if (!in_array($k1, $usedKeys)) {
$mergedRanges[] = $range1;
$usedKeys[] = $k1;
}
}
}
}
}
// If $ranges and $mergedRanges have the same # of elements,
// or if $ranges only had 1 element to begin with,
// that means we couldn't merge any more. Otherwise, recurse!
if (count($ranges) == 1)
return $ranges;
return count($mergedRanges) == 1 || (count($ranges) == count($mergedRanges))
? $mergedRanges
: combineRanges($mergedRanges);
}
/**
* Helper function to see if 2 ranges overlap.
*
* @param array $range1
* @param array $range2
* @return boolean
*/
function rangesOverlap(array $range1, array $range2)
{
// Find the day before each range in order to combine ranges
// that don't overlap but are right next to each other.
$overlap = false;
$range1Before = date('Y-m-d', strtotime('-1 day', strtotime($range1['from'])));
$range2Before = date('Y-m-d', strtotime('-1 day', strtotime($range2['from'])));
// Account for when $range1 is first or when $range 2 is first
if ($range1['from'] <= $range2['from'] && $range1['to'] >= $range2Before
|| $range2['from'] <= $range1['from'] && $range2['to'] >= $range1Before)
$overlap = true;
return $overlap;
}
关于php - 检查多个日期范围内更广泛的差距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27790758/
我无法消除 IE7 中列表项之间的差距。 HTML: row 1.1
我用 jquery mobile 开发了一个 Phonegap 应用程序。它是一个单页应用程序,具有固定的页眉、固定的页脚和从左侧打开的面板。内容 DIV 为白色,面板从其默认颜色加载。 问题是当面板
我在我当前的元素中使用了 bootstrap,它工作正常但只有一个故障: 我有 3 col-sm-6彼此相邻,当第二个col-sm-6比第一、第三长col-sm-6向左移动,第一个和第三个之间出现间隙
我目前正在创建一个网站。但是,我在缩小页面最右侧的边距差距时遇到了一些问题 As seen in this picture我尝试将 body 设置为 margin 0px,但这没有帮助。任何帮助都将不
我的 OpenLayers map 有问题。我无法让瓷砖一 block 挨着另一 block 。我已经检查过侵入式 CSS,但没有找到任何东西。有人可以帮我解决这个问题吗? 我目前的代码是这样的:
我在制作 时遇到了问题我页面的标题链接展开以填满整个 在我的网页中标记。下面似乎有一个缺口,我不知道如何修复它。 我的导航栏也有类似的问题,随着我增加浏览器的大小,它不断扩展。 尝试将鼠标悬停在这个
当使用 Xcode 11 beta 5 在 iOS 13 beta 6 上运行应用程序时,我在呈现搜索结果 View Controller 时遇到了奇怪的间隙: 以下是如何设置的一些内容: let s
(PostgreSQL 8.4) 继续我的 previous example ,我希望通过窗口函数进一步理解间隙和孤岛处理。考虑下表和数据: CREATE TABLE T1 ( id SERIAL
我正在开发 PWA 网站。必须通过单击主屏幕图标来启动应用程序。问题是将 iPhone 旋转到横向位置后出现 20px 间隙。这个间隙超出了 Window 对象,所以我无法用 js 或 css 处理它
我注意到当位置设置为绝对时,表格单元格垂直对齐不起作用。我做错了什么吗? 非工作 sample bottom alignament is
我尝试添加各种标签并尝试添加相对路径:'//' 这些是我尝试过的各种meta标签 我也尝试过为相对路径添加“//”。 execIframe.contentWindow.location
我有一个 CSS 嵌套菜单在 IE 8 和 Firefox 中完美运行,但在 IE7 中它会在元素之间产生一个小间隙。这是我的 CSS: #nav, #nav ul { margin: 0;
我正在尝试使用 SwiftUI 在 HStack 中有两个 View 。但是,我一直在两种观点之间存在差距。 该问题出现在纵向和横向布局中。 我的代码: struct ContentView: Vie
我的 svg 左侧有一个小缝隙,我一辈子都无法修复它。请参阅下图了解我所指的内容。在左侧,您可以稍微看到图像。 我在 Chrome 和 Firefox 上注意到这个问题。知道问题可能是什么吗? .aw
当我在 Visual Studio 2010 中创建一个全新的 ATL 项目时,资源 ID 102 和 105 被遗漏了。这是故意的还是错误?这些 ID 在项目后期有什么用吗?我可以重新排序编号以使用
我使用 JQuery Mobile+Phone Gap 实现了 TableView A Adam Kinkaid Alex Wickerham Avery Johnson B
我是一名优秀的程序员,十分优秀!