25

I have a sidebar where I want to show the most recent posts. Right now it shows the title, date, and an excerpt. The date shows the time which I want to get rid of. I show the date using this: $recent["post_date"]

 <?php
$args = array( 'numberposts' => '3' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
    echo '<li id="sidebar_text"><b>'.$recent["post_title"].'</b></li><li style="font-size:12px">'.$recent["post_date"].'</li><li><i style="font-size:15px">'.$recent["post_excerpt"].'</i><a href="'.get_permalink($recent["ID"]).'"> Read   More</a></li>';
     }
 ?>

It shows the date like this: 2013-08-11 18:29:04 and I would like it like this 8-11-2013 and without the time. Thanks in advance.

Mike
  • 430
  • 3
  • 7
  • 16

4 Answers4

51
date('n-j-Y', strtotime($recent['post_date']));

This formats it the way you want. Just replace the $recent['post_date'] in your loop with that.

ixchi
  • 2,349
  • 2
  • 26
  • 23
  • 1
    Syfaro's answer will get the desired result but if you want your site to consistently display dates in the same way you'd be better off changing the format of how dates are displayed in the 'Settings' > 'General' section (especially important if this is part of a plugin that you're distributing for other people to use as they'll want to see their local date format). You can then use the get_the_date function which by default doesn't return the time. Documented here: http://www.codesynthesis.co.uk/code-snippets/formatting-the-post-date-in-wordpress – Code Synthesis Aug 17 '14 at 11:51
  • 9
    use `get_option( 'date_format' )` to get the default date format as defined in Settings > General > Date Format – farinspace Jan 24 '18 at 03:06
8

Whilst Syfaro's answer is correct, best practice is to use WordPress's own function for this.

get_the_date

This defaults to the format set in the WordPress admin settings (Settings -> General), so gives a more accessible solution for future editing - particularly useful if you roll your code in multiple sites, or more importantly if you release it publicly.

Also, don't forget to escape output - check out esc_html and esc_html_e

Mark
  • 3,005
  • 1
  • 21
  • 30
5
date_i18n('l d/m/Y \à\s g:i', strtotime($item['time']))
3

Replace $recent["post_date"] with mysql2date('n-j-Y', $recent['post_date']).

Yamil Duba
  • 61
  • 4