2014-01-09 32 views
0

下面的代碼的結果給出:Januar 9 2014PHP轉換日期爲d-M-Y格式

怎麼可能使其向:9 Januar 2014

在此先感謝。

<?php 
    global $months; 
    $months = array('Januar', 'Februar', 'Marts', 'April', 'Maj', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'December'); 

    function convert_dates($text) 
    { 
     $date_regexp = '/([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})/'; 
     preg_match_all($date_regexp, $text, $dates); 
     $dates  = $dates[0]; 
     $new_dates = $dates; 

     for($i=0;$i<count($new_dates);$i++) { 
      $new_dates[$i] = explode('-', $new_dates[$i]); 
      $new_dates[$i] = $GLOBALS['months'][abs($new_dates[$i][1])-1] . ' ' . abs($new_dates[$i][2]) . ', ' . $new_dates[$i][0]; 
     } 

     $text = str_replace($dates, $new_dates, $text); 
     return $text; 
    } 
?> 
+0

它不是我想要的。我想使用上面的代碼,但我必須切換數字(日期)和文本(月)。 – RedHawkDK

+0

這不重複。這是完全不同的。 – Zlatan

+0

用'echo date('j。F Y')來試一試''(altrhought月份的名字是英文的) – Zlatan

回答

0

使用strftime格式化的日期,如果你不想英語,的strtotime從Y-M-d格式轉換爲一個時間戳。

<?php 
// this needs to happen before you call convert_dates 
// If your system default is already correct you do not need to do this 
setlocale(LC_ALL, 'da_DK.UTF-8'); 

function convert_dates($text) 
{ 
    $date_regexp = '/([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})/'; 
    preg_match_all($date_regexp, $text, $dates); 
    $dates  = $dates[0]; 
    $new_dates = $dates; 

    for($i=0;$i<count($new_dates);$i++) { 
     $new_dates[$i] = strftime('%e. %B %Y', strtotime($new_dates[$i])); 
    } 

    $text = str_replace($dates, $new_dates, $text); 
    return $text; 
}