2012-07-30 62 views
1

然而,對於大家另一個PHP日期的問題 - 我有一個英文版本的我的網站,其完美的作品當前代碼:PHP日期錯誤使用代碼當翻譯日期

$article = &$articles[$i]; 

if($currentdate != date("F Y", strtotime($article->getDate()))) { 
    $currentdate = date("F Y", strtotime($article->getDate()));     
    $current_content1 .= "<div class='press-articles-heading'>" . $currentdate . " </div>"; 
} 

基本上,這樣做是拉記錄從數據庫並比較日期,以便月份標題只顯示一次,例如2012年7月,然後所有月份的記錄,然後2012年6月等

如果我使用相同的代碼在非英文版本的網站,我得到了一個錯誤:

$article = &$articles[$i]; 

if($currentdate != date("F Y", strtotime($article->getDate()))) { 
    $currentdate = strftime("%B %Y", strtotime($article->getDate()));     
    $current_content1 .= "<div class='press-articles-heading'>" . $currentdate . "</div>"; 
} 

會發生什麼這裏是我獲得每個數據庫記錄的月頭,儘管他們是相同的。

看不到錯誤在哪裏,有沒有人有任何想法?

任何幫助,非常感謝。

更新

我沒有張貼問題之前嘗試一些東西,但認爲最好把它留出來以保持這個問題很簡單。

我曾嘗試以下操作:

$article = &$articles[$i]; 

if($currentdate != date("F Y", strtotime($article->getDate()))) { 
    $translated_date = strftime("%B %Y", strtotime($article->getDate()));     
    $current_content1 .= "<div class='press-articles-heading'>" . $translated_date . "</div>"; 
} 

但是這並沒有解決它

回答

1

試試這個:

$article = &$articles[$i]; 

$timeString = strtotime($article->getDate()); 
$numericDate = strftime('%m %Y', $timeString); 

if($currentDate != $numericDate) { 
    $currentDate = $numericDate; 
    $translatedDate = strftime('%B %Y', $timeString);     
    $current_content1 .= "<div class='press-articles-heading'>" . $translatedDate . "</div>"; 
} 

這將使根據年份和月份的數值攀比,但它會顯示本地化的文本值。我使用額外的變量來緩存一些日期分析結果。

+0

天才,謝謝! – martincarlin87 2012-08-08 08:52:08

0

你的date('F Y')值比較的strftime('%B %Y')值。那些可能不會匹配。