WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom date format using post_date (10 posts)

  1. koyder
    Member
    Posted 5 years ago #

    Hello everyone!
    I'd like to display post date outside the Loop. The only way I've been able to do that is with $post->post_date. However, it displays date in the following format: 2009-05-12 23:51:49, and I'd like to use a custom one (j M Y). How could I achieve that?

    Here's what it looks like on my site (I'm using a modified Smart Archives plugin): http://superpospolita.pl/archiwum/

    Any help would be greatly appreciated!

  2. Samuel B
    moderator
    Posted 5 years ago #

  3. koyder
    Member
    Posted 5 years ago #

    That seems to work only with the_date() and the_time(), which I can't use outside the Loop. But I've found a solution:

    mysql2date('j M Y', $post->post_date);

  4. Alkorr
    Member
    Posted 5 years ago #

    Hi koyder, thanks for sharing!

    But where do you exactly put mysql2date('j M Y', $post->post_date); to make it work properly? I do an echo on post_date to show the date but when I place mysql2date('j M Y', $post->post_date); into my code, it doesn't work...

    Thanks a lot for your help!

  5. ricardonunez
    Member
    Posted 4 years ago #

    Hi there,

    I am trying to do exactly the same thing but it does not seem to work.

    My code:

    echo "<li><a href=\"".get_permalink()."\">$post_title</a> - $post->post_date; ?></li>";

    What I get:

    “The title” - 2008-10-24 10:22:18

    What I want:

    “The title” - October 24, 2008

    If I change the code to:

    echo "<li><a href=\"".get_permalink()."\">$post_title</a> - mysql2date('j M Y', $post->post_date);</li>";

    I get:

    “The title” - mysql2date('j M Y', 2008-10-24 10:22:18);

    Thanks in advance for the help

  6. neologan
    Member
    Posted 4 years ago #

    you haven't wrapped the code, it's just plain text at the moment

    mysql2date('j M Y', $post->post_date);

    see?

    try:

    . mysql2date('j M Y', $post->post_date) .

  7. cheezepie
    Member
    Posted 4 years ago #

    I'm having the same problem...

    I'm trying to output:

    <li><a>Title</a> April 12, 2010</li>

    But when I use the code in neologan's example above I get:

    <li><a>Title</a> </li>

    Here's my code:

    '<li><a href="' . get_permalink($post["ID"]) . '" title="Read more of '.$post["post_title"].'" >' . $post["post_title"].'</a> '.$post["post_date"].' </li> '

  8. cheezepie
    Member
    Posted 4 years ago #

    Sorry, missed out the code including neologan's exmaple in my post above. The code including neologan's example looks like this:

    '<li><a href="' . get_permalink($post["ID"]) . '" title="Read more of '.$post["post_title"].'" >' . $post["post_title"].'</a> '. mysql2date('j M Y', $post->post_date) .' </li> '

    But outputs:

    <li><a>Title</a> </li>

    I'm trying to get:

    <li><a>Title</a> April 12, 2010</li>

  9. ian92484
    Member
    Posted 4 years ago #

    try this:

    . mysql2date('M j Y', $post["post_date"]) .

  10. cheezepie
    Member
    Posted 4 years ago #

    @ian92484

    That works perfectly! Thanks.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags