How to show each post’s date in WordPress

clockThe templating system in WordPress is very flexible, and there’s rarely something you can’t do or that doesn’t work as you’d expect. A notable exception however is the_date. Its purpose is simple enough. It displays the current post’s date of creation. But on pages with more than one post (such as on many blog home pages), something weird happens. If a number of posts on any given page were created on the same date, the_date will only show that date for the first of those posts.

In the early days of blogging, posts were usually listed by date, much like a regular, paper diary. In the old default theme that comes with WordPress a big date title is used to separate posts into days. That’s what the_date was created to do, and so it makes sense it only displays the same date once. In most modern themes however, people like the date to be among the meta data for each article, so the_date falls short.

the_time

In comes the_time. This template tag, as the name suggests, shows the time at which a post was created. However, it accepts a PHP time format string as an argument, and can be made to display the date. You could simply put a date formatting string in there and be done with it, but I like to keep things flexible, so i wanted to use the date format string set in WordPress’ settings. That way the user controls the output, and it can be adjusted to international preferences.

When I first ran into this I posted my issue on the WordPress forums, and Otto42 came up with a prefect solution. I’ve been using it ever since and thought I’d share it here.

the_time(get_option('date_format'));

What this does is get the user-set date format from WordPress and feed it to the_time. Since that function doesn’t have a ‘once a day’ limitation, you can use this as often as you like in your theme.

37 Comments

  1. very usefull, thanks

    Comment by Ali — December 27, 2009 @ 4:41 am

  2. This was very helpful, thanks. This exact solution didn’t work for me because I needed the date in another format, so I found this to work:

    Thought it might help those running into the same issue I did.

    Comment by Dax Davis — December 29, 2009 @ 7:20 am

  3. Good point but too techie. You don’t say exactly how to do it for us somewhat technically challenged.

    Comment by David — January 9, 2010 @ 11:57 pm

  4. David, this post is aimed at WordPress enthusiasts with some knowledge of theme editing. Perhaps I should have included some sort of disclaimer :). The code snippet goes anywhere inside the_loop.

    Comment by Roy — January 10, 2010 @ 10:36 am

  5. ha, easy does it. cheers mayn.

    Comment by michiel — January 22, 2010 @ 10:46 am

  6. nicely done!

    Comment by rudy — January 26, 2010 @ 12:40 am

  7. Exactly what i needed! thanks :)

    Comment by Asko — January 30, 2010 @ 10:25 pm

  8. Works great! Thanks a lot.

    Comment by AJ Clarke — February 3, 2010 @ 2:06 am

  9. Man you saved my life :). Tnx 4 sharing

    Comment by Igor — February 4, 2010 @ 5:21 pm

  10. Great Post!! Would love to add for those who would like to display the post date in WP sidebar

    3,
    ‘caller_get_posts’=>1
    );
    $my_query = new WP_Query($args);
    ( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <a href="” rel=”bookmark” title=”Permanent Link to “>

    Have a Great Day!

    Comment by Bobby — February 26, 2010 @ 4:15 am

  11. Link: Kako v Wordpressu prikažemo datum pri vsakem postu | Pomagalnik
  12. Looking for some way to remove the date from my static pages, i.e, home, about, etc. I am using the default classic theme. Any suggestions?

    Comment by Frank — March 13, 2010 @ 6:16 pm

    • Hi Frank. You’ll need to edit the theme somewhat, but it’s not very hard. You can either create a copy of index.php, call it page.php and remove the line where the_date() is called, or wrap that same line in an if-statement and use is_page() or is_single(). I realize this isn’t exactly a full tutorial, but Google will help you :).

      Comment by Roy — March 16, 2010 @ 4:46 pm

  13. thanks a lot, just what i was looking for!

    Comment by Lea — March 19, 2010 @ 2:37 pm

  14. Very useful. You saved me a lot of time. Thank you very much.

    Comment by 5-squared — March 23, 2010 @ 6:39 pm

  15. Thanks Roy, really saved me some head scratching. I figured it was happening because of multiple posts on the same day but didn’t know what to do about it!

    Comment by Peter Hobley — April 2, 2010 @ 9:03 pm

  16. Thanks~! Very useful.

    Comment by cooper — May 7, 2010 @ 9:14 am

  17. Perfect, Thanks.

    Comment by T — May 22, 2010 @ 3:37 am

  18. I had no idea why that was happening. Thanks a lot of point out why it happened and also the fix. Your the best!

    Comment by Jason — July 7, 2010 @ 12:51 am

  19. Yeeeeeha great!!! I think I love you (was a joke)

    Comment by Nillus — July 16, 2010 @ 2:19 pm

  20. Link: Start The Loops » [object HTMLImageElement]
  21. That solved a really annoying issue! Great stuff :)

    Comment by Marius Akerbæk — October 11, 2010 @ 12:45 pm

  22. Link: GoodDesign
  23. Thanks a Million. Exactly what I was trying to figure out!

    Comment by Kira Sherrine — February 17, 2011 @ 11:56 pm

  24. How to hide date and time shown on every WP posts without any plugins? Please help.
    Thanks in advance.

    Comment by bali spa — February 21, 2011 @ 6:39 am

  25. Many thanks for the date issue – this has helped a lot. However, I would also like to be able to show the time of each post or update which at present seems to adhere to the ‘once a day’ principle. I have a number of updates posted on the same day, but each of them shows exactly the same time.

    Using your code snippet above and modifying it slightly to get:

    the_time(get_option(‘date_format’));

    Still results in exactly the same time being assigned to each update.

    Can you advise how to fix this one too please?

    Thanks,

    Ben

    Comment by Ben Lloyd — February 23, 2011 @ 10:24 am

    • Hi Ben. the_time() should return the exact time of posting for each individual post. If it doesn’t, there’s probably something wrong with the loop in your theme, or soemthing else is interfering. There’s some much that theme and plugin developers can do that troubleshooting these things can be quite difficult.

      Comment by Roy — February 24, 2011 @ 11:09 am

  26. Hi Roy,

    The problem is actually to do with a widget that is collecting a Google Buzz feed. I thought maybe there might be a simple solution, but it would appear not!

    Thanks for your help,

    Ben

    Comment by Ben Lloyd — February 24, 2011 @ 12:02 pm

  27. How-To’s on the old comments of WordPress… nothing helpful for 2.7

    Comment by Martin — May 3, 2011 @ 1:04 am

  28. it works perfect for me..
    tks a lot!!

    best regards!
    ricardo

    Comment by ricardo — July 20, 2011 @ 6:18 pm

  29. Thanks for this helpful tip. I really appreciate it.

    Comment by sam — July 24, 2011 @ 4:00 am

  30. Great tip and yes this worked for me

    Comment by Julie Kitchens — July 24, 2011 @ 4:01 am

  31. You just saved the day :)
    cheers!

    Comment by malihu — August 31, 2011 @ 3:10 pm

  32. Sweet, thanks for the help!

    Cheers

    Comment by Dave — January 15, 2012 @ 5:39 am

  33. I think this is what I’m looking for but have no idea how to go about it. I’m very new to WordPress. I have a lovely theme which I want to keep, but what happens is whenever I post a new entry, the original date of the first entry stays on. I want the date of the latest entry to be displayed.
    Am I in the right place?
    Great! I thought so.
    However, all that “loop” business is eluding me.
    It appears you are tailing about placing a piece of code
    (namely the_time(get_option(‘date_format’));
    somewhere, but it’s that somewhere that I don’t know to get to.
    Would somebody be so kind as to explain it to me?
    Or perhaps point me in the right direction (like a youtube video or something?)
    Thanks people!

    Comment by vibhuti — January 31, 2012 @ 5:11 pm

    • The code in this post is indeed intended to go into the site’s theme. I noticed you entered a wordpress.com blog with this comment, and on wp.com there’s no way to edit your site’s theme. If you’re running a self-hosted blog, you can use the call to the_time to replace the call to the_date in the main loop. Depending on the theme, this loop can be in several files, but index.php is usually a good place to start.

      Comment by Roy — February 1, 2012 @ 10:35 am

  34. Exactly what I needed – thank you so much!

    I was using the_date and it wasn’t working at all – I slapped your code in and it worked perfectly. Thanks Roy!

    Comment by Matt M — February 25, 2012 @ 12:43 am