2016-12-23 27 views
1

爲什麼此代碼無法按計劃運行?多次加入月份不起作用

代碼:

$regDate = '2016-10-03'; 
    echo $date1 = date('m', strtotime('+4 month', strtotime($regDate))); echo '<br>'; 
    echo $date2 = date('m', strtotime('+4 month', strtotime($date1))); echo '<br>'; 
    echo $date3 = date('m', strtotime('+4 month', strtotime($date2))); echo '<br>'; 
    echo $date4 = date('m', strtotime('+4 month', strtotime($date3))); echo '<br>'; 

我回來:

+0

你有沒有使用'YYYY-MM-DD'格式的日期試過嗎? –

+0

我需要它只給月 –

回答

1

這是無法識別的日期。你需要先計算日期然後echo他們。

$regDate = '2016-10-03'; 
$date1 = strtotime('+4 month', strtotime($regDate)); 
echo date('m', $date1) . '<br>'; 
$date2 = strtotime('+4 month', $date1); 
echo date('m', $date2) . '<br>'; 
$date3 = strtotime('+4 month',$date2); 
echo date('m', $date3) . '<br>'; 
$date4 = strtotime('+4 month', $date3); 
echo date('m', $date4) . '<br>'; 

另一種簡單的方法是 - - 與嘗試

$regDate = strtotime('2016-10-03'); 
foreach(range(1, 4) as $val) { 
    $regDate = strtotime('+4 month', $regDate); 
    echo date('m', $regDate) . '<br>'; 
} 

輸出

02 
06 
10 
02 
+0

這是它的感謝! –

+0

用循環檢查方法。這更容易和更簡單。 –

+0

非常感謝:) –