【问题标题】:Format dates in a WordPress plugin在 WordPress 插件中格式化日期
【发布时间】:2009-08-25 14:26:53
【问题描述】:

我正在修改最近更改 WordPress 插件以显示日期。我可以回显日期但无法格式化;例如,mm/dd/yyyy。

我希望 post_modified 日期为 mm/dd/yyyy。

我试过了--

echo '<li>'.$RecentChange->post_modified('m/d/Y'). 

--但这会导致插件停止显示帖子,并且通常会破坏网站。

下面是插件中的相关块--

/* define full SQL request */
$rc_sql = "SELECT post_date, post_modified, post_title, ID FROM wp_posts WHERE ".$rc_content_sql." ORDER BY post_modified DESC LIMIT ".$rc_number;

global $wpdb;
echo $before_widget;
echo $before_title.$rc_title.$after_title.'<ul>';
$RecentChanges = $wpdb->get_results($rc_sql);

if ($RecentChanges)
foreach ($RecentChanges as $RecentChange) :
$rc_url = get_page_link($RecentChange->ID);
echo '<li>'.$RecentChange->post_modified.' <a href='.$rc_url.'>'.$RecentChange->post_title.'</a></li>';
endforeach;
echo '</ul>'.$after_widget;
$wpdb->flush(); 
}

【问题讨论】:

  • 我使用的是 WordPress 2.8.4。 PHP 版本为 4.3.9。

标签: wordpress


【解决方案1】:

试试

<?php
    mysql2date('m/d/Y', $RecentChange->post_modified);
?>

见reference。

【讨论】:

    【解决方案2】:

    假设RecentChanges->post_modified 是一个PHP 日期或时间,您可以将其包装在PHP 的日期函数中并按照您的需要进行格式化。

    date("m/d/Y", $RecentChanges->post_modified);
    

    所以,你的行应该是这样的:

    echo '<li>'.date("m/d/Y", $RecentChanges->post_modified).' <a  href='.$rc_url.'>'.$RecentChange->post_title.'</a></li>';
    

    您的代码很可能会破坏 WordPress,因为 post_modified 函数只是一个 getter,不带参数。

    【讨论】:

    • 它确实显示了一个日期,但日期是 1969 年 12 月 31 日。当我不格式化 post_modified 时,我确实得到了准确的日期。这是进步——网站没有坏掉。
    猜你喜欢
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-29
    • 2017-02-13
    相关资源
    最近更新 更多