• Skip to primary navigation
  • Skip to main content

[m]utuality

WordPress Development, Support & Site Care

  • How Does It Work?
  • Services
  • About Us
    • Ethics
  • Show Search
Hide Search

Change post date format to time ago in WordPress

Time ago format used on popular social networks could be a good alternative to default WordPress post date format for websites publishing lots of content very frequently.

Using function below you can display post publish date in “time ago” format like “3 seconds ago”, “7 minutes ago”, “6 hours ago”, “1 week ago”, etc.

WordPress provide us with function human_time_diff which determines the difference between two timestamps (e.g. the time post was published and current time) and return result in a human readable “time ago” format such as “1 hour”, “4 days”, “1 month”.

We can use human_time_diff function directly in our templates but if we wrap it in a function it would be much easier to reuse it later no matter where in the code we may need it.


/* Function which will return post date in time ago format */
function mtly_time_ago() {
	return human_time_diff( get_the_time( 'U' ), current_time( 'timestamp' ) ).' '.__( 'ago' );
}


/* Using our function inside the loop */ 
if ( have_posts() ): /* check if we have posts */
	while ( have_posts() ): the_post(); /* while we have posts iterate the post index in the loop */
	the_title(); /* post title */
	echo mtly_time_ago(); /* display post date in time ago format */
	the_content(); /* display post content */
	;
endif;

Written by:
admin
Published on:
March 23, 2018

Explore more

We're here to listen and help.

Share your aspirations and challenges with us,
and let's work together to create the perfect solution.