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

3 minutes read

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));
  else
    $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.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To get yesterday&#39;s date in MySQL, you can use the DATE_SUB() function. This function subtracts a specified time interval from a date.The syntax of the DATE_SUB() function is as follows: DATE_SUB(date, INTERVAL value unit) Here, date is the current date, an...
You might have noticed that some WordPress blogs show a last modified or last updated date in place of the published date on the posts or pages. If you’re looking to add the same functionality to your blog too, this tutorial will going to help you to achieve ...
To change a date format in MySQL, you can use the DATE_FORMAT() function. The syntax of the function is as follows: DATE_FORMAT(date, format) The date parameter represents the date column or value that you want to format, and the format parameter specifies the...