如何查找数组中两个日期之间的日期

时间:2019-03-22 15:13:17

标签: php arrays date date-range

我有一个包含开始日期和结束日期的数组。现在,我想将给定日期之间的各个日期保存在数组中。

Array
(
[0] => Array
    (
        [start] => 2019-02-16
        [end] => 2019-02-23
    )

[1] => Array
    (
        [start] => 2019-03-15
        [end] => 2019-03-17
    )

[2] => Array
    (
        [start] => 2019-03-23
        [end] => 2019-03-24
    )

[3] => Array
    (
        [start] => 2019-03-27
        [end] => 2019-03-29
    )

[4] => Array
    (
        [start] => 2019-04-17
        [end] => 2019-04-21
    )
)

这应该是结果:(仅“之间”的天)

Array
( 
 '2019-03-16',
 '2019-03-28',
 '2019-04-18',
 '2019-04-19',
 '2019-04-20'
)

如何遍历数组?

1 个答案:

答案 0 :(得分:2)

将其应用于每个日期并合并为数组:

$from = DateTime::createFromFormat('Y-m-d', '2019-02-16');
$from->add(new DateInterval('P1D'));
$to = DateTime::createFromFormat('Y-m-d', '2019-02-23');
$dates = iterator_to_array( new \DatePeriod( $from ), new \DateInterval( 'P1D' ), $to ) );