If you use Twitter and Facebook, you may have noticed the dynamic timestamp of tweets and posts in the stream in minutes, hours and days (see below screenshot). Once in a while, you want the same feature on your WordPress blog (i.e. WordPress time format in days, hours and minutes ago). Well, this is pretty much a coder’s thing, but you can do it without being a coder as well – all you need to do is modifying some code in your WordPress theme template.

Display dynamic Time in WordPress like twitter and facebook

Below are the two methods, Method 1 for common users and Method 2 for advance users. Common users can also implement Method 2 if they have a basic knowledge of PHP.

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

Method 1: Simple

WordPress themes implement <?php the_time(); ?> function (in index.php and single.php generally) to display post time. Just use the below function in place of this default time function and notice the changes (simply edit index.php and single.php files of your WordPress theme, find and select the full function i.e. <?php the_time( … ); ?>, replace it with the below code and save 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: Advance

The way it displays “time ago” when a post is published, in number of days / hours / minutes, it’s cool. But most of you would say, it’s not cool when it shows 200 or 300 days ago for older posts.

dynamic time format in wordpress

Here is the solution of that also. Those who are not satisfied with the above simple modification results, should add the below code in the functions.php file of their WordPress theme at the end, just before ?>, and save it:

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 changes.

If you are feeling difficulties while modifying your theme to achieve this, drop in a comment, I’ll help you.