Show Post Time Twitter Style

Snippets, Tutorials | By | 3 February 2011 | Comments 0

WordPress by default will show the date of the post which in most cases is sufficient, but say your WordPress install has frequent daily posts or you use it for a different purpose, such as a “FML” clone this method will not suffice.

What we will do today is show the post dates like Twitter does, eg: 24 minutes ago, 4 hours ago etc…

This snippet will be added to your functions.php file (so backup before you begin) and you then have a snippet which you can use where you need to see the time, for example single.php, index.php etc…

functions.php

Add the following into your functions.php file, this function grabs the time and converts it into “Twitter time.”

function twitter_time() {
echo "This Was Posted ".human_time_diff(get_the_time('U'), current_time('timestamp'))." ago";
}

This snippet can be placed wherever you need the Twitter style time to display:

<?php twitter_time(); ?>

You can alter this snippet to read whatever way you wish by changing the echoed line, for example:

echo "about ".human_time_diff(get_the_time('U'), current_time('timestamp'))." ago";

Conclusion

This is a really straightforward snippet to add to your install, it portrays a more human approach to displaying post dates



Paul Maloney is a UK based web designer/developer and runs the design agency Tropica. He particularly enjoys using and working with WordPress and has a keen interest in typography. You can also follow Paul on Twitter.

Visit Paul Maloney's website

Be The First To Comment

Leave A Comment.