search ]

Display Time Since Post Was Published in WordPress

A small snippet from the WordPress Codex that displays the time since a post was published.
Add the following code to your functions.php file:


/**
 * Display time since post was published
 *
 * @uses	human_time_diff()  Return time difference in easy to read format
 * @uses	get_the_time()  Get the time the post was published
 * @uses	current_time()  Get the current time
 *
 * @return	string  Timestamp since post was published
 *
 */
function get_time_since_posted() {

	$time_since_posted = 'Posted ' . human_time_diff( get_the_time( 'U' ), current_time( 'timestamp' ) ) . ' ago';

	return $time_since_posted;

}

Then add the function inside the loop where you want it:

echo get_time_since_posted();

For another useful post meta display, see Display Estimated Post Reading Time.

Join the Discussion
0 Comments  ]

Leave a Comment

To add code, use the buttons below. For instance, click the PHP button to insert PHP code within the shortcode. If you notice any typos, please let us know!

Savvy WordPress Development official logo