翻译日期格式


Translate date format

我有以下一段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 替换日期并尝试将区域设置设置为丹麦语,但在某种程度上,当将日期格式更改为 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') ) { ?>
...

您进行双日期转换:1)时间戳到文本; 2)文本到时间戳。而您不需要任何转换。您可以简单地使用get_post_meta在语句中获得的时间戳if。此外,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);
        ...                        
    }
    ...

由于时区差异,日期值可能被错误地翻译。

尝试类似的东西

// $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++; }

此代码避免转换计算日期,但会这样做以显示。