2012-03-16 33 views
0

我試圖創建一個從今天到數據庫中最舊記錄的月份列表。 它工作得很好,除了循環在到達最早的月份之前停止。這裏是我的代碼:PHP通過博客歸檔的月份循環

echo $oldest_entry; //2012-01-31 
$end = strtotime($oldest_entry); 
$month = strtotime(date('Y-m-d')); 
$year = ""; 
while($month >= $end) 
{ 
    if(date('Y', $month) != $year){ 
     echo "<b>".date('Y', $month)."</b><br/>"; 
     $year = date('Y', $month); 
    } 
    echo date('F', $month)."<br/>"; 
    $month = strtotime("-1 month", $month); 
} 

它輸出:

並沒有得到到一月。我在這裏做錯了什麼?我認爲將=添加到>會解決這個問題,但事實並非如此。

回答

4

它不打印一月,因爲2012-01-16 >= 2012-01-31是錯誤的。

您應該省略日期。在比較中僅使用年份和月份。