2014-12-03 37 views
0

我需要遍歷日期以獲得每個塊爲1天的塊。這樣我總共需要10天。我試過這段代碼,但那不起作用。循環10天,並將每個日期存儲在一個數組中

for($i=0; $i<=10;$i++) 
    { 
     $dates=date("Y-m-d", strtotime($current_date, "+1 days")); 
     $tmp_array[$i]=$dates; 
     debug($date); 
    } 

我得到這個打印無限次的錯誤。

Notice: A non well formed numeric value encountered in /Path/to/the/file on line 45 
2014-12-03 
+0

請張貼工作完全源碼e片段。您當前的代碼片段無法重現錯誤,因爲它缺少'$ current_date'。 – Evert 2014-12-03 05:44:23

回答

0
for($i=0; $i<=10;$i++) 
    { 
     $temp = strtotime("+$i day"); 
     $dates=date("Y-m-d", $temp); 
     $tmp_array[$i]=$dates; 

    } 
    print_r($tmp_array); 

//輸出

Array ([0] => 2014-12-03 [1] => 2014-12-04 [2] => 2014-12-05 [3] => 2014-12-06 [4] => 2014-12-07 [5] => 2014-12-08 [6] => 2014-12-09 [7] => 2014-12-10 [8] => 2014-12-11 [9] => 2014-12-12 [10] => 2014-12-13) 
1

這應該爲你工作:

for($i = 0; $i <= 10; $i++) 
    $dates[] = date("Y-m-d", strtotime("+$i days")); 

echo "<pre>"; 
print_r($dates); 

輸出:

Array 
(
    [0] => 2014-12-03 
    [1] => 2014-12-04 
    [2] => 2014-12-05 
    [3] => 2014-12-06 
    [4] => 2014-12-07 
    [5] => 2014-12-08 
    [6] => 2014-12-09 
    [7] => 2014-12-10 
    [8] => 2014-12-11 
    [9] => 2014-12-12 
    [10] => 2014-12-13 
) 
相關問題