Error in adding to 2d array or looping through 2d array

后端 未结 1 1740
深忆病人
深忆病人 2021-01-26 01:17

I have a problem in this code:

while ($end <= $to){
        $currentDates = array(\"from\" => $start, \"to\"=>$end);
        $allDates[] = $currentDates         


        
相关标签:
1条回答
  • 2021-01-26 01:54

    I don't think you need 2 loops for that ....

    The error is from your loop

    while ($end <= $to){
                    ^-------  This was never used
    

    Also See

    $currentDates = array("from" => $start, "to"=>$end);
             Not in the Condition  --^              ^---- To means something else 
    

    You while can be as simple as

    $start = new DateTime("2012-4-12");
    $end = new DateTime("2012-12-12");
    $dv = new DateInterval('P24D'); // Every 24 days
    
    echo "<pre>";
    while ( $start <= $end ) {
        echo "From ", $start->format('Y-m-d');
        $start->add($dv);
        echo " To ", $start->format('Y-m-d'), PHP_EOL;
    }
    

    Output

    From 2012-04-12 To 2012-05-06
    From 2012-05-06 To 2012-05-30
    From 2012-05-30 To 2012-06-23
    From 2012-06-23 To 2012-07-17
    From 2012-07-17 To 2012-08-10
    From 2012-08-10 To 2012-09-03
    From 2012-09-03 To 2012-09-27
    From 2012-09-27 To 2012-10-21
    From 2012-10-21 To 2012-11-14
    From 2012-11-14 To 2012-12-08
    From 2012-12-08 To 2013-01-01
    
    0 讨论(0)
提交回复
热议问题