Skip to content

WordPress Improved Human Time Difference

WordPress has a built in function, called human_time_diff, for creating messages that say how long ago a post was published – however I think it is confusing when you have posts from a long time ago that read “posted 15 months ago” since you then have to calculate the date.

So I thought I’d make an enhanced version of human_time_diff that checks how long ago the post was published and changes to the publication date if the post is older than 60 days. This way you get the nice clean ‘3 hours ago’ style message – or the clearer date listing on older content.

function bm_human_time_diff_enhanced( $duration = 60 ) {

	$post_time = get_the_time('U');
	$human_time = '';

	$time_now = date('U');

	// use human time if less that $duration days ago (60 days by default)
	// 60 seconds * 60 minutes * 24 hours * $duration days
	if ( $post_time > $time_now - ( 60 * 60 * 24 * $duration ) ) {
		$human_time = sprintf( __( '%s ago', 'binarymoon'), human_time_diff( $post_time, current_time( 'timestamp' ) ) );
	} else {
		$human_time = get_the_date();

	return $human_time;


echo bm_human_time_diff_enhanced();

Ben View All

Ben is a lifelong Nintendo fan who also likes to build websites, and develop games. He also buys way too much Lego.

8 thoughts on “WordPress Improved Human Time Difference Leave a comment

  1. Pingback: WordPress Improved Human Time Difference
    • ah ha – good catch. I must have written the comment wrong 🙂 90 days it is. Also an easy fix if you do want it to be 60 days.

      Thanks for pointing it out – I shall correct the code.

  2. Very informative. I’d modified this code to my blog. This is really important to calculate publishing time. I didn’t see anybody who is using this so this is the first time I’ve found this here. Thank you in advance for this code.

  3. Hi,

    We used your code a lot! But are now bumping into a problem. As we start using a cache plugin, the time ago only updates when the cache is cleared. And that a bummer. Is it somehow possible to alternate this to an ajax based one?

    • thank – you’re right that this wouldn’t work so well with caching plugins. To be honest I hadn’t really considered that. It’s definitely possible to do this with javascript but it’s not something I have time to do myself. Personally if I were to write is in javascript I would output a standard WordPress time and date, and add a data property that has the post timestamp. Then I’d use javascript to replace the time and date with the human time diff calculated by the timestamp.

  4. Hi Ben,

    thank you for your response, and i understand it. It’s a very nice code and tutorial.
    I think i found a plugin that we can try out. So hopefully that would work.

Leave a Reply

Your email address will not be published. Required fields are marked *