2017-11-11 121 views
-2

我在一個正在製作日期的while循環中有一個變量。我希望將while循環的每次迭代中生成的所有日期轉換爲while循環外的數組。這可能嗎?如何使while循環中的迭代值數組超出

while($row=mysql_fetch_array($sq)) 
    { 
    extract($row); 
    $startDate = date("d-m-Y", strtotime($created_on. ' + '.$days_for_start.' days')); 
    $end_date = date("d-m-Y", strtotime($created_on. ' + '.$days_for_end.' days')); 
    } 

print_r($end_date); 

//should output, 24-03-2017 for number of times the loop is running, in an array. 
+0

inside'while' loop use this'$ end_date [] = ...' –

+0

使用數組例如'$ end_dates = [];'循環前和'$ end_dates [] =日期...'循環內然後'print_r($ end_dates)'外循環 – apokryfos

+0

對不起,沒有成功:(@SahilGulati – Ansh

回答

1
$end_date = array(); 
while($row=mysql_fetch_array($sq)) 
{ 
    extract($row); 
    $startDate = date("d-m-Y", strtotime($created_on. ' + '.$days_for_start.' days')); 
    $end_date[] = date("d-m-Y", strtotime($created_on. ' + '.$days_for_end.' days')); 
} 

print_r($end_date); 

更新一個

//用來$end_date_tmp變量作爲數組。

$end_date_tmp = array(); 
while($row=mysql_fetch_array($sq)) 
{ 
    extract($row); 
    $startDate = date("d-m-Y", strtotime($created_on. ' + '.$days_for_start.' days')); 
    $end_date_tmp[] = date("d-m-Y", strtotime($created_on. ' + '.$days_for_end.' days')); 
} 

print_r($end_date_tmp); 
+0

對不起,但不工作。其實我不能把end_date放到while循環裏面的數組中嗎? – Ansh

+0

分享你的代碼和數據比我可以幫你... –

+0

添加代碼 – Ansh