smackaysmith
smackaysmith

Reputation: 133

Format dates in a WordPress plugin

I'm modifying the Recent-Changes WordPress plugin to display dates. I can echo the date but can't format it; e.g., mm/dd/yyyy.

I'd like the post_modified date to be in mm/dd/yyyy.

I've tried--

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

-- but that caused the plugin to stop displaying posts, and generally broke the site.

Below is the relevant chunk 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(); 
}

Upvotes: 3

Views: 1987

Answers (2)

UncleZeiv
UncleZeiv

Reputation: 18488

Try

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

See reference.

Upvotes: 5

eddieroger
eddieroger

Reputation: 523

Assuming RecentChanges->post_modified is a PHP date or time, you could wrap it in PHP's date function and format it how you want.

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

So, your line would look like this:

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

It's likely that your code is breaking WordPress because the post_modified function is just a getter and doesn't take parameters.

Upvotes: 1

Related Questions