简体   繁体   English

查找数组中的重叠日期范围

[英]Finding overlaping date ranges in an array

I have an array of date ranges like this: 我有一个这样的日期范围数组:

[0] => Array
                (
                    [start_time] => 2011-10-01 00:00:00
                    [end_time] => 2011-10-05 00:00:00
                    [name] => Apples
                )

[1] => Array
                (
                    [start_time] => 2011-10-04 00:00:00
                    [end_time] => 2011-10-10 00:23:00
                    [name] => Oranges
                )

[2] => Array
                (
                    [start_time] => 2011-10-15 00:00:00
                    [end_time] => 2011-10-20 00:23:00
                    [name] => Bananas
                )

I'm trying to calculate the overlap between each event and 'split' this overlap into a separate item in the array, then adjust the start_time and end_time of the intersecting events accordingly, so they no longer overlap. 我正在尝试计算每个事件之间的重叠并将此重叠“拆分”到数组中的单独项目中,然后相应地调整相交事件的start_time和end_time,以便它们不再重叠。 For example, in the array above 'Apples' intersects with Oranges by one day, so I'd like to end up with an array that looks like this. 例如,在上面的数组'苹果'与橘子相交一天,所以我想最终得到一个看起来像这样的数组。

[0] => Array
                (
                    [start_time] => 2011-10-01 00:00:00
                    [end_time] => 2011-10-04 00:00:00
                    [name] => Apples
                )
[1] => Array
                (
                    [start_time] => 2011-10-04 00:00:00
                    [end_time] => 2011-10-05 00:00:00
                    [name] => Apples Oranges
                )

[2] => Array
                (
                    [start_time] => 2011-10-05 00:00:00
                    [end_time] => 2011-10-10 00:23:00
                    [name] => Oranges
                )

[3] => Array
                (
                    [start_time] => 2011-10-15 00:00:00
                    [end_time] => 2011-10-20 00:23:00
                    [name] => Bananas
                )

To get reasonably efficient code, I guess that you will have to order the dates by beginning time. 为了获得合理有效的代码,我想您必须按开始时间订购日期。 After that, it should be fairly easy to get the intersections by walking over the end-dates. 在那之后,通过走过结束日期来获得交叉点应该相当容易。

As you said that you were new to PHP, you might want to read up on the function http://php.net/manual/en/function.strtotime.php This converts your formatted dates into unix timestamps, thus making them comparable. 正如你所说的你是PHP的新手,你可能想要阅读函数http://php.net/manual/en/function.strtotime.php这会将你的格式化日期转换为unix时间戳,从而使它们具有可比性。

For sorting your above data structure, have a look at the different sorting functions for arrays: http://de3.php.net/manual/en/array.sorting.php 要对上面的数据结构进行排序,请查看数组的不同排序函数: http//de3.php.net/manual/en/array.sorting.php

Especially uasort might be interesting for you. 特别是uasort可能对你有意思。

As the others said, SO is not here to write your code, so have a look at set theory to get an idea for even better performing solutions. 正如其他人所说的那样,SO并不是在编写代码,因此请查看集合论以获得更好的解决方案。

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

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