Create an array/collection/list of (Date,Value) in PHP

前端 未结 3 1565
我在风中等你
我在风中等你 2021-01-26 12:50

I am writing a PHP script where the inputs are:

From date
To date

I then want to take that date range and create an array of some sort that has

3条回答
  •  囚心锁ツ
    2021-01-26 13:14

    (Untested)

    function dateArray($from, $to, $value = NULL) {
        $begin = new DateTime($from);
        $end = new DateTime($to);
        $interval = DateInterval::createFromDateString('1 day');
        $days = new DatePeriod($begin, $interval, $end);
    
        $baseArray = array();
        foreach ($days as $day) {
            $dateKey = $day->format("Y-m-d");
            $baseArray[$dateKey] = $value;
        }
    
        return $baseArray;
    }
    
    $datesArray = dateArray('2011-01-01', '2011-03-31',true);
    

提交回复
热议问题