Human Readable Date in WordPress: X Minutes/Days/Years ago

Some simple ways to show the WordPress date and time in human readable form; i.e. in “X minutes/days/years ago” format with the human_time_diff() function.

We all are well aware of that dynamic, human readable timestamp; and the best example demonstrating it are the tweets and posts in our social media feeds. Below is a screen capture that flaunts the beauty of that timestamp showing itself off in minutes, hours and days.

Display dynamic Time in WordPress like twitter and facebook

Now, you want the same thing on your WordPress blog—i.e. you want your WordPress posts with dynamic date and time—in days, hours and minutes ago format. This is pretty much a coder’s thing, but this post is all about making this task dead simple. So, no coding skills required to be frank. All you’ll be doing is modifying some code in your WordPress theme’s functions template.

We are going to cover two methods here; Method #1 with the least possible code mess, and Method #2 with some advanced features with a good bit of code.

Note: Before proceeding further, backup your WordPress theme to be on the safer side.

Method #1: Simple

WordPress themes implement <?php the_time(); ?> function (generally in the index and single post templates) to display the post time. Just replace <?php the_time();> function in those templates (index.php, single.php, and/or archive.php) with the one give below and save the changes.

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

If you have implemented the above replacement correctly, you must be seeing the post timestamp as shown highlighted in the below screenshot:

change wordpress time format

Method #2: Advanced

The way method #1 displays the time is cool. Now, we have our post timestamp in number of days / hours / minutes ago format. But I would say, it’s not that cool when it shows 200 or 300 days ago for older posts.

WordPress date: time ago format

Let’s get rid of that too, with just a small block of code. Those who are not satisfied with the simple modification results in method #1, should add the below code in the functions.php file of their WordPress theme at the very end, and save the changes.

add_filter('the_time', 'dynamictime');
function dynamictime() {
  global $post;
  $date = $post->post_date;
  $time = get_post_time('G', true, $post);
  $mytime = time() - $time;
  if($mytime > 0 && $mytime < 7*24*60*60)
    $mytimestamp = sprintf(__('%s ago'), human_time_diff($time));
    $mytimestamp = date(get_option('date_format'), strtotime($date));
  return $mytimestamp;

The above code will make <?php the_time(); ?> function to display minutes, hours and days ago format for the posts which are published in the last 7 days. For the posts older than 7 days, it will show the normal date format.

display dynamic time stamp in wordpress

Note: If you have implemented Method #1 before Method #2, revert the changes (i.e. use the default time function <?php the_time(); ?> only). Check your blog to observe the changes.

Okay, so you don’t want to deal with the code editing at all. Is there a plugin to do the job for you? The Time Ago is my recommendation.

If you are feeling difficulties while modifying your theme to achieve this, drop in a comment, I’ll help you. Don’t forget to read about showing last modified date in WordPress posts.