Forums

Date formatting in a foreach echo (6 posts)

  1. Shaun1123
    Member
    Posted 1 year ago #

    Hi Everyone,

    It echos the post date but I need to format it. The date looks like this: 2010-11-17 21:05:00
    But I need it to look like this: Oct 14 6:45pm

    $number_recents_posts = 5;
     $recent_posts = wp_get_recent_posts( $number_recents_posts );
     foreach($recent_posts as $post){echo '<li><a href="' .get_permalink($post["ID"]). '" title="Look '.$post["post_title"].'" >' .$post["post_title"].'</a><p>'.$post["post_date"].'</p></li>'
     ;}

    I really appreciate your help...

  2. Nelson
    Member
    Posted 1 year ago #

    Go into the Administration page and click on Settings (on the bottom left)
    and on the page you are now viewing check the Date Format and the Time Format.
    I think you want the Date format to be the first selection and the Time format to be the first selection as well.

    Hope that answers your question.

  3. Shaun1123
    Member
    Posted 1 year ago #

    Hi Sandilee,
    thank you for your help. I changed the setting but with no joy. Any other ideas?

  4. David Sader
    Member
    Posted 1 year ago #

    You'll still need the date_format option applied to your post_date.

    Something like this maybe.

    <?php echo date_i18n(get_option('date_format'), $post['post_date']); ?>
    or in your syntax
    ...<p>'.date_i18n(get_option("date_format"), $post["post_date"]).'</p>...

    I think date_i18n is what you'll need, or mysql2date.

    http://codex.wordpress.org/Formatting_Date_and_Time

  5. Shaun1123
    Member
    Posted 1 year ago #

    Hi David,
    Thanks for your input, alas no luck my friend. all I get now is Jan 1 for every post. I also tried:

    echo '<li><a href="' .get_permalink($post["ID"]). '" title="Look '.$post["post_title"].'" >' .$post["post_title"].'</a><p>'.the_time('M jS g:i a').'</p></li>'

    Which echos the right format but out side of the p tag I put it in and the wrong date. It's really weird...
    Should I use a different loop?

  6. David Sader
    Member
    Posted 1 year ago #

    or mysql2date.

    <?php
    $number_recents_posts = 5;
     $recent_posts = wp_get_recent_posts( $number_recents_posts );
     foreach($recent_posts as $post){echo '<li><a href="' .get_permalink($post["ID"]). '" title="Look '.$post["post_title"].'" >' .$post["post_title"].'</a><p>'.mysql2date(get_option("date_format"), $post["post_date"]).'</p></li>'
     ;}
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic