Showing a Wordpress post’s ‘last modified’ date

I’ll occasionally return to a post and revise it for improved methodology, test results or whatever. But the ‘posted on’ date always remains the same, even after the post has been updated. I feel that displaying only the ‘posted on’ date could be somewhat confusing, particularly when I state that I’ve updated the post in a comment dated months later. So, in the interest of full disclosure, I have added a few lines of code to the WordPress ’single.php’ template file to supplement each post’s meta data with the date it was last modified. If the post has never been modified or if it was last modified within 24 hours of the ‘posted on’ date, only the ‘posted on’ date is shown.

Of course, this could be used anywhere inside the Wordpress loop, not just in the meta data section. The code I use to show a Wordpress post’s last modified date and time is as follows.

The default Kubrick template’s meta data section:

<p class="postmetadata alt">
<small>
This entry was posted 
...
on <?php the_time('l, F jS, Y') ?> 
at <?php the_time() ?>
and is filed under <?php the_category(', ') ?>.
You can follow any responses to 
this entry through the 
<?php comments_rss_link('RSS 2.0'); ?> feed. 

The new code, modified to selectively display the last modified date:

<p class="postmetadata alt">
<small>
This entry was posted
...
on <?php the_time('F jS, Y') ?> 
at <?php the_time() ?>
						
<?php $u_time = get_the_time('U'); 
$u_modified_time = get_the_modified_time('U'); 
if ($u_modified_time >= $u_time + 86400) { 
echo "and last modified on "; 
the_modified_time('F jS, Y'); 
echo " at "; 
the_modified_time(); 
echo ", "; } ?>
	
and is filed under <?php the_category(', ') ?>.
You can follow any responses to 
this entry through the 
<?php comments_rss_link('RSS 2.0'); ?> feed. 

You can see how this works in the meta data section of this post.

Further customization

I’m using a grace period of 24 hours from the time the post was published, but you could change this by replacing 86400 with however much time you want, specified in seconds.

  • email
  • Facebook
  • Digg
  • StumbleUpon
  • del.icio.us
  • Google Bookmarks
  • Technorati
  • LinkedIn
  • Reddit
  • MySpace
  • Slashdot
  • SphereIt
  • Sphinn
  • Mixx

22 Responses to “Showing a Wordpress post’s ‘last modified’ date”

  1. [...] date” without erasing the “creation date” of the post and I found this great solution from Ardamis.It requires a modification of your WordPress template. The code below was given by Ardamis and [...]

  2. [...] Showing a Wordpress post’s “last modified” date: nothing rocket science, just something that’s a nice idea to implement in your theme. Hey, I just did ;) Share This [...]

  3. [...] en ardamis.com (con ejemplo para el tema Kubrick que viene por defecto en [...]

  4. [...] Ardamis – Showing a WordPress Posts Last Modified Date [...]

  5. Bruce says:

    Ardamis, thanks for this code snippet: I really can’t understand why there isn’t such a function built in to WP. You say “If the post has never been modified, only the posted-on date is shown”. It looks like I modify a lot of my posts on the day of publishing e.g. adding “alt” tags, correcting spelling. I’m a perpetual PHP beginner but I’d like to display the following code if modified date > post date by 24 hrs – to allow for such trivial edits (and it makes no sense displaying a post date and modification date that are the same anyway):

    <?php /* Last modified */ $u_time = get_the_time('U'); $u_modified_time = get_the_modified_time('U'); if ($u_modified_time != $u_time) { echo "<span class='lastmod'>Last modified "; the_modified_time('F jS, Y'); echo "</span>"; } ?>

    Is this possible, or am I looking at a plugin to do this?

  6. ardamis says:

    Wow. Yeah, I should have thought of that when I wrote the code. I’m just the same, always catching a typo or spelling error a moment after publishing a post, so many of my posts have a modified date that is the same as the published date.

    I’ve updated the code to allow for a 24-hour grace period after publishing a post, during which any editing won’t show up as a “modification”.

    Thanks for the great comment. This blog is better for it.

  7. [...] order to do this, Ardamis has posted a quick code hack to get your single pages to reflect the last modified date. He uses the default Kubrick theme, but [...]

  8. [...] Do you often edits your posts and want your readers to know about it? With this tutorial from Ardamis, you can now display the last modified date of your WordPress posts. [...]

  9. [...] Do you often edits your posts and want your readers to know about it? With this tutorial from Ardamis, you can now display the last modified date of your WordPress posts. [...]

  10. Marcelo says:

    Dude! Great feature! Specially for news sites. Thanks, works like a charm.

  11. [...] order to do this, Ardamis has posted a quick code hack to get your single pages to reflect the last modified date. He uses the default Kubrick theme, but [...]

  12. Melissa says:

    Thank you so much for this :)

  13. PokerAnon says:

    I do a lot of updating at later times, usually just formatting the modifications in italics, but this is great. Although, now it shows all the times I’ve made grammatical corrections as well. :)
    Do you think this helps the modifications to be picked up by search engines? Sometimes I rewrite 1/3 of the post and would like the revision to be noticed.

  14. [...] via Ardamis’s Blog, there is a great way to only display this information if it has been modified after the original [...]

  15. [...] I found this great little code snipped at ardamis.com [...]

  16. morten says:

    Nice – this really helped me. Do you happen to know why this doesn’t work for pages? It seems that get_the_modified_time() only returns modified time for post, and not pages.
    I’d like to show a little “Updated” icon next to the pages that has been changed after a 10 day period, but I can’t figure out how to get the modified time for pages :-/

  17. ardamis says:

    Hey there, Morten.

    Strange that it’s not working for you on Pages. I use the_modified_time('l, j F Y'); on each of my Pages to display the date it was last changed, and it works.

    I haven’t tested, but the_modified_time() simply filters get_the_modified_time(), so I’d assume that get_the_modified_time() must work.

    Try putting in the_modified_time('l, j F Y'); where you want your icon, just to be sure that the last changed date is appearing.

  18. [...] I was thinking about it and I came across a site called Adramis where I found this [...]

  19. [...] surfing di dunia maya, akhirnya saya menemukan kode ini, tentunya dengan sedikit penyesuaian script nya. Caranya [...]

  20. Darren says:

    Thanks for this! Clean and straightforward code. Exactly what I was looking for.

  21. Owen says:

    Using this snippet on a site I just built.

    Thanks for everything!

    (link is same as ‘website’)

  22. Hugo Lim says:

    hey, thanks alot, am using this now!

Leave a Reply

Wrap code snippets in <code></code> tags.