2014-04-01 206 views
0

我有下面這段PHP的,通過我的wordpress的帖子循環:轉換日期格式

<?php 
    $items = 0; 
    $thiscat = get_category(2); 
    $cat_slug = $thiscat->slug; 
    $args = array( 
     'post_type' => 'Course', 
     'category_name' => $cat_slug, 
     'order' => 'ASC', 
     ); 
    $loop = new WP_Query($args); 
    while ($loop->have_posts() AND $items < 3) { 
     $loop->the_post(); 
     $category_course = get_the_category(2); 
     $cat_slug_course = $category_course[0]->slug; 
     $date_start = get_post_meta(get_the_ID(), 'date_start', true); 
     $place = get_post_meta(get_the_ID(), "place", true); 
     $date_start = date("D, M d, Y", $date_start); 
     if(strtotime($date_start) >= strtotime('today')) { ?> 
      <li>        
      <a href="<?php the_permalink(); ?>" class="date_fp"><?php echo $date_start; ?> - <?php echo $place; ?></a> 
      </li> 
<?php $items++; } 
    } 
    if($items==0) { ?> 
     <li>        
      Ingen kommende kurser 
     </li> 
<?php } ?> 

它循環並顯示多達三個的開始日期的課程。不過,我希望$date_start以丹麥語而不是英語輸出。

我試着用strftime更換日期,並試圖建立locale丹麥,但在某種程度上,更改日期格式時STRFTIME(%A,%d%B,%Y),我的循環只是輸出Ingen kommende kurser(無未來課程)。這很奇怪,因爲在使用日期(並獲取英文輸出)時,會顯示課程日期。

$ DATE_START輸出以毫秒爲單位

一個解決方案,我試過的時間(例如1371081600137535),但沒有奏效:

... 
$place = get_post_meta(get_the_ID(), "place", true); 
setlocale(LC_ALL, 'nl_NL'); 
$date_start = strftime("%a, %d %b, %Y", mktime($date_start)); 
if(strtotime($date_start) >= strtotime('today')) { ?> 
... 
+0

您確定您的系統上安裝了所需的區域設置嗎? – nils

回答

1

你讓雙日期轉換:1)日期和時間爲文本; 2)文本到時間戳。而你不需要任何轉換。您可以簡單地使用if聲明中的get_post_meta獲取的時間戳。 另外setlocale僅影響格式化,不影響時區。如果你想申請丹麥時區,你必須撥打date_default_timezone_set

試試這個:

... 
    $date_start = get_post_meta(get_the_ID(), 'date_start', true); 
    $place = get_post_meta(get_the_ID(), "place", true); 
    date_default_timezone_set('Europe/Copenhagen'); 
    if($date_start >= strtotime('today')) { 
     ...       
     echo date("D, M d, Y", $date_start); 
     ...       
    } 
    ... 
0

這是可能的日期值被錯誤地轉換由於時區的差異。

嘗試像

// $date_start is already a timestamp so do not do conversion 
// $date_start = date("D, M d, Y", $date_start); 
if($date_start >= strtotime('today')) { ?> 
<li>        
    <a href="<?php the_permalink(); ?>" class="date_fp"><?php echo date("D, M d, Y", $date_start); ?> - <?php echo $place; ?></a> 
</li> 
<?php $items++; } 

此代碼避免轉換日期計算,但會顯示這樣做。