2014-09-29 58 views
1
<table class="projects"> 
    <tr><td class="title" colspan="2">Vergangene Projekte</td></tr> 

<?php $query = "SELECT * FROM posts WHERE project = 1 && project_date <= CAST(CURRENT_TIMESTAMP AS DATE)ORDER BY date DESC"; 
     if(!$query){ 
     die("Konnte nicht mit Data Base verbinden");} 
     $result = mysqli_query($connection, $query); 

while($row = mysqli_fetch_array($result)){ 

     setlocale(LC_ALL, 'de_DE'); // using german language works 
     $premier = new DateTime($row['project_date']); 
     echo "<tr><td>" .strftime("%B", $premier->getTimestamp()). "</td><td class=\"project_title\"> 
     <a class=\"title\" href=\"projekt_info.php?projekt=" .urlencode($row["id"]) ."\">" 
     .$row['title']. "</a></td></tr>";} 
?> 
</table> 

好的我修改了代碼來顯示所有的代碼,並插入了建議,現在它工作得很好!完美如何更改日期語言?

乾杯 克里斯

+0

你可以嘗試添加'setlocale(LC_TIME,'fr_FR');' – 2014-09-29 13:53:22

+0

另外,是你的完整代碼?如果是這樣,就會有不少事情丟失。表標籤,例如數據抓取。 – 2014-09-29 13:57:07

+0

並確保'fr_FR'語言環境確實安裝在機器中。 'locale -a' – Ghost 2014-09-29 13:59:24

回答

3

首先確保確實fr_FR安裝在機器上的腳本中使用。

嘗試在終端上使用locale -a進行檢查。

其次,由於DateTime只適用於英文,因此請使用strftime(),並且您沒有使用時間戳。

setlocale(LC_ALL, 'fr_FR'); 
$premier = new DateTime($row['project_date']); 
echo "<tr><td>" .strftime("%B", $premier->getTimestamp()). "</td><tr>"; 
            // ^^ feed the timestamp 

旁註:弗雷德在評論中說,這僅僅是一個片段,因爲你的標記是有點過(缺少的標記等,並最有可能的,這是一個循環內)。

0

有點晚了,但我更喜歡使用IntlDateFormatter這是系統區域設置獨立的輸出是保證在UTF-8:

$datefmt = new IntlDateFormatter('de_DE', NULL, NULL, NULL, NULL, 'LLLL'); 
while($row = mysqli_fetch_array($result)) { 
    # ... 
    echo "<tr><td>" . $datefmt->format($premier). "</td>"/*...*/; 
    # ... 

(見ICU對a custom format文檔 - IntlDateFormatter構造函數的最後一個參數)