Format dates in WordPress plugin

I am modifying the WordPress Recent-Changes plugin to display dates. I can repeat the date, but I can not format it; e.g. mm / dd / yyyy.

I want the post_modified date to be in mm / dd / yyyy.

I tried -

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

- but this caused the plugin to stop displaying messages and generally broke the site.

Below is the corresponding snippet from the plugin -

/* 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(); 
}
+3
source share
2 answers

Try

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

See reference .

+5
source

Assuming RecentChanges-> post_modified is a PHP date or time, you can wrap it in a PHP date function and format it the way you want.

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

So your line will look like this:

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

, WordPress, post_modified .

+1

Source: https://habr.com/ru/post/1716000/


All Articles