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

前端 未结 3 1566
我在风中等你
我在风中等你 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:09

    If I understand you correctly, you could use an associative array for that:

    array(
      '00-00-00' => $value,
      '01-01-01' => $value,
      // etc...
    );
    

    Or you can create it like this:

    $myArray = array();
    $myArray['00-00-00'] = $value;
    $myArray['01-01-01'] = $value;
    

    You could populate them by running a loop...

    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2021-01-26 13:26

    you can try this

    function makeDateRange($from,$to,$pattern='m-d-y')
    {
        $day = date( $pattern , $from );
        $to = date( $pattern , $to );
        $parseDate = date_parse($from);
        while( $day <= $to ) {
            $day = mktime(0,0,0,$parseDate["month"],$parseDate["day"]+1,$parseDate["year"]);
            $dateArray[] = date($pattern , $day);
            }
        return $dateArray;
    }

    // here make array $keys = makeDateRange("12-01-11","12-02-11");

    //here make above array as key in $a array $a = array_fill_keys($keys, 'none'); print_r($a);

    0 讨论(0)
提交回复
热议问题