Add a Human Readable Time Difference Stamp to Your WordPress Site

Add a Human Readable Time Difference Stamp to Your WordPress Site


Much like Facebook, Twitter, and countless media outlets, WordPress has a function that allows you to specify how long ago a post was published in days (or hours if it was recently published). So, for example, instead of having your posts say, “Posted on Feb 6, 2012,” you could have them say, “Posted 6 days ago.”

Or, you could have say both.

How to Do It

In order to get this function on your site, you will need to edit your theme’s files and put the following piece of code where you want it to appear. Go to Appearance > Editor > (choose the file you want to edit).

<?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'; ?>

So, for example, I found where the date was out put on the default Twenty Eleven theme, and I edited it. Instead of replacing the date, I decided to add the time differential function to the end of the section for the date and the author.

Also, because I was using both the full date and the time differential, I decided I would put the time differential in parenthesis. To do that, I simply put the whole piece of code in parenthesis.

(<?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'; ?>)


And here’s the result.

As mentioned, if you recently published a post, it will show up in hours.

Comments

If you’d like use this in your comments, then find the date function in your comments file, and insert the following code.

<?php echo human_time_diff( get_comment_time('U'), current_time('timestamp') ) . ' ago'; ?>

If you’re comfortable with coding and would like to know more about the human time differential function, you can find it here.

Days Ago Plugin

I did find a plugin (wp-days-ago) that does the same thing, but it also offers some more complicated options that some more experienced users may be interested in.

Photo: Calendar from BigStock

 

Free Video Why 100 is NOT a Perfect Google PageSpeed Score (*5 Min Watch) Learn how to use Google PageSpeed Insights to set realistic goals, improve site speed, and why aiming for a perfect 100 is the WRONG goal.
Tags: