在WordPress插件中格式化日期

时间:2009-08-25 14:26:54

标签: wordpress

我正在修改Recent-Changes 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(); 
}

2 个答案:

答案 0 :(得分:5)

尝试

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

请参阅reference

答案 1 :(得分:1)

假设RecentChanges-&gt; 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而且不带参数。