在PHP中用日期数组填充空日期 [英] Fill empty dates in array of dates in PHP

查看:60
本文介绍了在PHP中用日期数组填充空日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在codeigniter中有一个数组,如下所示.如下所示,日期之间有一些空格.4月1日之后,是第3个,之后是第10个,依此类推.

I have an array in codeigniter like below. As shown below, there are some spaces between dates. After 1st April, there is 3rd and after that its 10th and so on.

 Array
    (
        [2019-04-01] => 1
        [2019-04-03] => 3
        [2019-04-10] => 8
        [2019-04-15] => 1
    )

我需要那些空日期的值为'0'的空日期,所以最后,数组将如下所示:

I need those empty dates to be there with a value '0' so at last, the array will look like:

    Array
            (
       [2019-04-01] => 1
       [2019-04-02] => 0
       [2019-04-03] => 3
       [2019-04-04] => 0
       [2019-04-05] => 0
       [2019-04-06] => 0
       [2019-04-07] => 0
       [2019-04-08] => 0
       [2019-04-09] => 0
       [2019-04-10] => 8
       [2019-04-11] => 0
       [2019-04-12] => 0
       [2019-04-13] => 0
       [2019-04-14] => 0
       [2019-04-15] => 1
            )

数组的第一个和最后一个 index 将作为范围.

The first and last index of array will work as range.

到目前为止我尝试过的事情

我正在使用这种方法,但这似乎不起作用.在下面的代码中, testArr 是上面发布的数组

What I've tried so far

I was using this approach but this doesn't seem to work. In below code, testArr is the array posted above

                $i=0;    
                foreach ($testArr as $key => $value) {

                if($i==count($testArr)-1){break;}

                $currentDay=new DateTime($key);
                $nextDay=new DateTime(array_keys($testArr)[$i]);


                $diff=date_diff($nextDay,$currentDay);
                $diffint=intval($diff->format("%a"));
                if($diffint!==1){
                    $currentDay->modify('+1 day');
                    $temp=array($currentDay->format('Y-m-d')=>0);
                    $testArr = $this->insertArrayAtPosition($testArr,$temp,$i);
                }

                $i++;
            }


如果该问题在某处已经有答案,那么不要忘记将其标记为重复.


If this question already has answers somewhere, then don't forget to mark it as duplicate.

推荐答案

您可以使用 DatePeriod 获取数组的第一个键和最后一个键之间的范围.

You can use DatePeriod to get the range between the first key and the last key of the array.

$arr = array(
    "2019-04-01" => 1,
    "2019-04-03" => 3,
    "2019-04-10" => 8,
    "2019-04-15" => 1,
);

$keys = array_keys( $arr );
$start = new DateTime($keys[0]);
$stop = new DateTime($keys[ count( $keys ) - 1 ]);
$stop->modify('+1 day');

$range = new DatePeriod($start, new DateInterval('P1D'), $stop);
$result = array();

foreach ($range as $key => $value) {
    $date = $value->format('Y-m-d');

    if ( isset( $arr[ $date ] ) ) $result[ $date ] = $arr[ $date ];
    else $result[ $date ] = 0;
}

这将导致:

Array
(
    [2019-04-01] => 1
    [2019-04-02] => 0
    [2019-04-03] => 3
    [2019-04-04] => 0
    [2019-04-05] => 0
    [2019-04-06] => 0
    [2019-04-07] => 0
    [2019-04-08] => 0
    [2019-04-09] => 0
    [2019-04-10] => 8
    [2019-04-11] => 0
    [2019-04-12] => 0
    [2019-04-13] => 0
    [2019-04-14] => 0
    [2019-04-15] => 1
)

这篇关于在PHP中用日期数组填充空日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆