Partly as an excercise in trying to write a plugin for WordPress, and partly because I like the idea of having “posted 2 hours ago” on my blog but none of the existing plugins for WP did what I wanted, I have written this plugin.

The plugin consits of a single function, which returns the approximate time, in words, in English, since an event (The event I am using on the front page is the post date) . The text returned is something like “just minutes ago”, “an hour ago”, “two days ago”, “many years ago”, obviously depending on the length of time passed.

WARNING: I am not very good at PHP, so while it appears to work on my blog, I give no guarantee that it will work on yours. The usual caveats of backing up your posts, or installing on a test server first apply.

To use this plugin, copy the plugin script source to a plain text file, and save as about_ago.php in your WP plugins directory. Once this is enabled, you can call the function in a similar method to the way I have.

Posted < ?php
if(function_exists('drn_about_ago'))
echo drn_about_ago($post->post_date_gmt);
else
{

echo ' at ';
the_time('H:i on F jS, Y');
} ?>

Or more simply

 < ?php if(function_exists('drn_about_ago'))
echo drn_about_ago($post->post_date_gmt); ?>

This should be adaptable to other events, such as when comments were posted etc. Note that this function takes a date-time string, not a unix timestamp.

Hopefully this is some use to someone other than myself, and if you have any suggestions, or improvements, please post a comment or trackback this post.

Update: Please make sure when you are copying the PHP source that there are no spaces before the < ?php at the start or after the ?> at the end, otherwise you will get errors when you enable the plugin about headers.

No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URL

Sorry, the comment form is closed at this time.