从数组中获取日期范围

时间:2016-01-20 11:44:12

标签: php

我有以下日期数组:

Array
(
    [0] => 2016-02-01
    [1] => 2016-02-02
    [2] => 2016-02-03
    [3] => 2016-02-04
    [4] => 2016-02-05
    [5] => 2016-02-06
    [6] => 2016-02-07
    [7] => 2016-02-08
    [8] => 2016-02-09
    [9] => 2016-02-13
    [10] => 2016-02-14
    [11] => 2016-02-15
    [12] => 2016-03-13
    [13] => 2016-03-14
    [14] => 2016-03-15
    [15] => 2016-03-16
    [16] => 2016-03-17
    [17] => 2016-03-18
    [18] => 2016-03-19
    [19] => 2016-04-19
    [20] => 2016-04-20
    [21] => 2016-04-21
    [22] => 2016-04-22
)

如何获得形成范围的数组日期。像

Array
(
    [0] => 2016-02-01
    [1] => 2016-02-02
    [2] => 2016-02-03
    [3] => 2016-02-04
    [4] => 2016-02-05
    [5] => 2016-02-06
    [6] => 2016-02-07
    [7] => 2016-02-08
    [8] => 2016-02-09
)

Array
(
    [0] => 2016-02-13
    [1] => 2016-02-14
    [2] => 2016-02-15
)

Array
(
    [0] => 2016-03-13
    [1] => 2016-03-14
    [2] => 2016-03-15
    [3] => 2016-03-16
    [4] => 2016-03-17
    [5] => 2016-03-18
    [6] => 2016-03-19
)

我列出的主阵列中的日期可以是任何顺序。我提供的示例所有日期范围都可以识别。但raay是动态的,可以按随机顺序排列日期。如果该数组,我想要的日期范围。我生气了。请帮忙。提前谢谢。

1 个答案:

答案 0 :(得分:0)

您有以下代码。请试试这个。

    $array = Array(0 => '2016-02-01',
    1 => '2016-02-02',
    2 => '2016-03-19',
    3 => '2016-02-04',
    4 => '2016-02-05',
    5 => '2016-03-18',
    6 => '2016-02-07',
    7 => '2016-02-08',
    8 => '2016-02-09',
    9 => '2016-02-13',
    10 => '2016-02-14',
    11 => '2016-02-15',
    12 => '2016-03-13',
    13 => '2016-03-14',
    14 => '2016-03-15',
    15 => '2016-03-16',
    16 => '2016-03-17',
    17 => '2016-02-06',
    18 => '2016-02-03',
    19 => '2016-04-19',
    20 => '2016-04-20',
    21 => '2016-04-21',
    22 => '2016-04-22');

//echo "<pre>";print_r($array);
sort($array);

$newDateArray = array();
$i = 0;
$arrayCount = count($array);
foreach($array as $key=>$val) {
    //echo $key;
    if(isset($array[$key+1])) {
        $date1 = $val;
        $date2 = $array[$key+1];
        $newDateArray[$i][] = $date1;
        $datetime1 = date_create($date1);
        $datetime2 = date_create($date2);
        $interval = date_diff($datetime1, $datetime2);

        if($interval->days > 1) {
            $i++;
        }
    }
    else if($arrayCount == ($key+1)) {
        $newDateArray[$i][] = $date2;
    }
}
echo "<pre>";print_r($newDateArray);exit;

您的结果如下。

Array
(
    [0] => Array
        (
            [0] => 2016-02-01
            [1] => 2016-02-02
            [2] => 2016-02-03
            [3] => 2016-02-04
            [4] => 2016-02-05
            [5] => 2016-02-06
            [6] => 2016-02-07
            [7] => 2016-02-08
            [8] => 2016-02-09
        )

    [1] => Array
        (
            [0] => 2016-02-13
            [1] => 2016-02-14
            [2] => 2016-02-15
        )

    [2] => Array
        (
            [0] => 2016-03-13
            [1] => 2016-03-14
            [2] => 2016-03-15
            [3] => 2016-03-16
            [4] => 2016-03-17
            [5] => 2016-03-18
            [6] => 2016-03-19
        )

    [3] => Array
        (
            [0] => 2016-04-19
            [1] => 2016-04-20
            [2] => 2016-04-21
            [3] => 2016-04-22
        )

)

现在您可以轻松拆分阵列了。并得到你真正想要的结果。 祝你有个美好的一天!!! ...随意提出更多疑问...