简体   繁体   English

将一组日期分成连续的日期数组

[英]split up an array of dates into consecutive date arrays

i have a array of dates that looks something like this: 我有一系列日期,看起来像这样:

Array (
  [0] => 08/20/2013
  [1] => 08/21/2013
  [2] => 08/22/2013
  [3] => 08/23/2013
  [4] => 08/26/2013
)

it's always going to be different depending on the dates the user selects but for example lets use this one. 它总是会有所不同,具体取决于用户选择的日期,例如让我们使用这个。

what i need to do is figure out a way to separate the array into consecutive dates and non consecutive dates. 我需要做的是找出一种方法将数组分成连续日期和非连续日期。

so in the end i should have something like: 所以最后我应该有类似的东西:

Array (
  [0] => 08/20/2013
  [1] => 08/21/2013
  [2] => 08/22/2013
  [3] => 08/23/2013
)

Array ([4] => 08/26/2013)

i should specify that it's not only two arrays. 我应该指定它不仅仅是两个数组。 non consecutive dates would each have their own array, and consecutive dates would each be in their own array. 非连续日期每个都有自己的数组,连续日期各自都在自己的数组中。

Using $arr to represent your array: 使用$arr表示你的数组:

usort($arr, function ($a, $b){
    return strtotime($a) - strtotime($b);
});
$out = array();
$last = 0;
$dex = -1;
foreach ($arr as $key => $value){
    $current = strtotime($value);
    if ($current - $last > 86400) $dex++;
    $out[$dex][] = $value;
    $last = $current;
}
print_r($out);

Output: 输出:

Array
(
    [0] => Array
        (
            [0] => 08/20/2013
            [1] => 08/21/2013
            [2] => 08/22/2013
            [3] => 08/23/2013
        )

    [1] => Array
        (
            [0] => 08/26/2013
        )

)

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

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