2015-04-06 146 views
0

我需要一個列表,它通過幾個月迭代,並且總是顯示像上次天這個減少月時間戳PHP

2015-04-30 
2015-03-31 
2015-02-28 
... 

我的想法是與strtotime方法,其中,「1430344800」是時間步長要做到這一點根據2015年4月30日

$time_temp = 1430344800; 
echo date('Y-m-t',$time_temp)."<br>"; 

$time_temp = strtotime("-1 month",$time_temp); 
echo date('Y-m-t',$time_temp)."<br>"; 

$time_temp = strtotime("-1 month",$time_temp); 
echo date('Y-m-t',$time_temp)."<br>"; 

,但我只是得到

2015-04-30 
2015-03-31 
2015-03-31 

replaci ng'Y-m-t'by'Y-m-d'給出

2015-04-30 
2015-03-30 
2015-03-02 

爲什麼它不能正確地減少月份,我該如何實現呢?

+0

迭代使用一個月的___first___,因爲每個月都會有至少1天,然後使用't'爲您顯示 –

+0

字串'-1 month'不neccessarely得到較上月 – adeneo

+0

你@adeneo那是什麼呢?! – Adam

回答

0
$lastDayOfMonth = time(); // depending on what you're trying to do, this could change. 
// For example, it could be = strtotime("+1 month"); 
for($i = 0; $i < $numberOfMonthsToShow; $i++){ 
    $lastDayOfMonth = strtotime("last day of previous month", $lastDayOfMonth); 
    echo date('Y-n-j', $lastDayOfMonth).'<br />'; 
}