2015-06-03 74 views
1

我試圖讓我的第二段代碼的輸出是荷蘭語。 第一個代碼給出正確的輸出,但是用英文。我試圖用荷蘭語得到它,但通常我最終會與01-01-1970,12-12-2015或沒有結果。 本週的代碼應該給下週五,除非它的過去星期一,然後它是下週星期五。PHP:更改日期語言/ strftime/strtotime

我的工作代碼,但英文。

<?php 
$d = strtotime('today'); 

switch ($d) { 
    case strtotime('monday'): 
    case strtotime('friday'): 
    case strtotime('saturday'): 
    case strtotime('sunday'): 
     $ff = strtotime('next friday'); 
     $ffn = date('l d M', $ff); 
     echo $ffn; 
    break; 
    case strtotime('tuesday'): 
    case strtotime('wednesday'): 
    case strtotime('thursday'): 
     $ff = strtotime('next friday' . '+ 7days'); 
     $fft = date('l d M', $ff); 
     echo $fft; 
    break; 
    default: 
     echo "Something went wrong"; 
} 
?> 

我的代碼,給出了一個荷蘭人輸出,但錯誤的日期(它給今天的日期,而不是下週五/週五到下週。)

順便說一句,這是我的第一個問題IVE在這裏問,所以我可能會有點模糊或什麼;)

+0

將您的荷蘭日期轉換爲英文,然後使用php的strtotime函數獲取下一個星期五,然後將其轉換回荷蘭語,也許php有一個本地化的日期;我不確定,但是您可以輕鬆地通過替換日/月字符串從/到荷蘭語/從英語 –

+0

@NikosM。感謝您的回覆,我會試一試(: – JeroenM

+0

)您可以使用strftime()以任何語言輸出日期http://php.net/manual/en/function.strftime.php爲什麼你不返回正確的timestamp在你的switch語句中,然後用strftime()來顯示荷蘭語的日期? –

回答

3

只輸出使用的strftime()和set_locale日期()函數。 。無需更改代碼的邏輯是否正確。

http://php.net/manual/en/function.strftime.php

http://php.net/manual/en/function.setlocale.php

<?php 
$d = strtotime('today'); 

$format = '%A %d %b'; 
setlocale(LC_TIME, 'NL_nl');  
setlocale(LC_ALL, 'nl_NL'); 

switch ($d) { 
    case strtotime('monday'): 
    case strtotime('friday'): 
    case strtotime('saturday'): 
    case strtotime('sunday'): 
     $ff = strtotime('next friday'); 
     $ffn = strftime($format, $ff); 
     echo $ffn; 
    break; 
    case strtotime('tuesday'): 
    case strtotime('wednesday'): 
    case strtotime('thursday'): 
     $ff = strtotime('next friday' . '+ 7days'); 
     $fft = strftime($format, $ff); 
     echo $fft; 
    break; 
    default: 
     echo "Something went wrong"; 
} 
?> 
+0

感謝,作品像一個魅力(: – JeroenM

2

現在我用str_replace代替。這對我來說是一種簡單而快速的方式來獲得我需要的輸出。但是我現在只進行了近一年的編程,所以我的解決方法可能不是最好的。但現在工程(! 感謝eveyone誰回覆了線程

這就是我得到了現在使用str_replace函數

<?php 
$d = strtotime('today'); 
setlocale (LC_TIME,'NL_nl'); 
ini_set('intl.default_locale', 'nl_NL'); 
switch ($d) { 
    case strtotime('monday'): 
    case strtotime('friday'): 
    case strtotime('saturday'): 
    case strtotime('sunday'): 
     $ff = strtotime('next friday'); 
     $ffn = date('l d F ', $ff); 
     $dutch = str_replace (
      array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December', 'Friday'), 
      array('Jan', 'Feb', 'Mrt', 'Apr', 'Mei', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dec', 'Vrijdag'), 
      $ffn); 
     echo $dutch; 
    break; 
    case strtotime('tuesday'): 
    case strtotime('wednesday'): 
    case strtotime('thursday'): 
     $ff = strtotime('next friday' . '+ 7days'); 
     $fft = date('l d F', $ff); 
     $dutch = str_replace (
      array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December', 'Friday'), 
      array('Jan', 'Feb', 'Mrt', 'Apr', 'Mei', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dec', 'Vrijdag'), 
      $fft); 
     echo $dutch; 
    break; 
    default: 
     echo "Something went wrong"; 
} 
?> 
1

strftime()是支持本地化的唯一PHP日期&時間功能。

date()strtotime()僅使用英文。

爲了得到幫助strftime()可以幫助您,您首先需要使用setlocale()

不幸的是,setlocale()的第二個參數不是標準化的,它的值在一定程度上取決於運行PHP代碼的操作系統。

在類Unix系統(Linux,OSX),使用:

/* Set locale to Dutch */ 
setlocale(LC_TIME, 'nl_NL'); 

/* Output: woensdag 3 juni 2015 */ 
echo strftime("%A %e %B %Y", strtotime('2015-06-03')); 

在Windows中,使用:

/* Set locale to Dutch */ 
setlocale(LC_TIME, 'nld_nld'); 

上面的代碼片斷來自的setlocale()文檔複製。我改變了日期並測試了第一個例子(在Mac OSX上),但我手邊沒有一個Windows系統來檢查第二個例子是否像廣告一樣工作。無論如何,我可以從過去的經驗中得知,你需要在Windows上使用的字符串與在類Unix系統上使用的字符串不同。如果您需要在Windows上運行代碼,您可以在documentation頁面的末尾找到有用的鏈接。

+0

感謝您的答案,但是我用這種方法得到的問題是,我的代碼沒有給出正確的日期輸出,但我明白,通常這種方式工作,沒有搞砸結果。爲我工作,沒有搞亂我的結果(到目前爲止)所以我認爲我會繼續使用str_replace。但至少我知道如何strftime()等工程(:thnx – JeroenM