php中的多个时间交集
我现在遇到了一个我无法理解的情况。多个时间重叠。我看过很多关于两个重叠时间的帖子,但没有看到多次重叠的帖子。
这是我想要放入的数据:
<?php
//RUN EACH Class Selection for Monday
foreach($class as $Classes){
$time[] = array($ClassStartTime, $ClassEndTime, $classID);
}
OverLapFunction($time);
?>
然后将发布 classID 和重叠金额。以前有人遇到过这种情况吗?或者知道如何做到这一点?
I have a situation that I can not wrap my head around at the moment. Multiple Time Overlaps. I've seen many posts on two times that overlap but nothing that has multiple times overlapping.
Here's the data I would like to place in:
<?php
//RUN EACH Class Selection for Monday
foreach($class as $Classes){
$time[] = array($ClassStartTime, $ClassEndTime, $classID);
}
OverLapFunction($time);
?>
Which would then post the classID and Overlapping Amount. Has anyone run into this situation before? Or figured out how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您的数据是这样的:
您只需要一个嵌套的
foreach
,如下所示:基本上,它将每个类与所有类进行比较。如果
$class1
开始时间小于$class2
结束时间并且$class1
开始时间大于$class2
开始时间:它们重叠。请记住,每个类将相互比较两次(例如:A 到 B、B 到 A),因此如果第一次不匹配,则第二次会匹配。这将为您提供:
如果您要将类
D
更改为:...有效地重叠所有类,您将得到:
Say your data is like this:
You just need a nested
foreach
, like this:Basically, it compares each class to all classes. If
$class1
start time is less than$class2
end time and$class1
start time is more than$class2
start time: they overlap. Keep in mind that each class will be compared to each other twice (e.g.: A to B, B to A), so if it doesn't match on the first pass, it will in the second.This'll give you:
If you were to change class
D
to:...effectively overlapping all classes, you'd get:
免责声明:此函数更多是伪代码,您需要自己实现;它返回重叠类的组。我这样写是为了让你更好地理解。如果您需要更多解释为什么我们需要首先对时间进行排序,请告诉我。
哦,郑重声明一下:如果您有一个用户选择了某些类别,并且您需要确保他选择的类别中没有两个重叠,您可以将问题简化为您已经掌握的问题:
注意:上面的问题如果您按开始日期对项目进行排序并仅检查两个连续的数组元素是否重叠,则可以提高性能。
Disclaimer: This function is more pseudocode and you'll need to implement it yourself; It returns groups of overlapping classes. I've written it like this for you to understand it better. If you need more explanation on why we need to sort the times first, let me know.
Oh, and for the record: if you have a user that selects some classes and you need to ensure that no two of his chosen classes overlap, you can reduce the problem to the one you already have a grasp on:
Note: the problem above can be made better performance-wise if you sort the items by starting date and just check if two consecutive array elements overlap.