Show Post Time Twitter Style

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

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

[php]
function twitter_time() {
echo "This Was Posted ".human_time_diff(get_the_time(‘U’), current_time(‘timestamp’))." ago";
}
[/php]

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

[php]
<?php twitter_time(); ?>
[/php]

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

[php]
echo "about ".human_time_diff(get_the_time(‘U’), current_time(‘timestamp’))." ago";
[/php]

Conclusion

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



11 Comments

Leave A Comment.