For the last couple of weeks I’ve been working on a massive WordPress project that has required a lot of interesting and unusual components that step well outsie of what is normally expected from WordPress and its plugins. Once the project is live I’ll write a longer article about all these components, but in the meantime I’m going to share some of the functions because I think you might find them useful.
“Published X Days Ago”
The first function I’ll feature is a small script that spits out a text telling the viewer how long ago a post was posted. I actually needed this script for a countdown so mine is a fair bit longer, but it can be used to create a Twitter-like time stamp just as easily.
To achieve this we’ll be using two functions, date() and get_the_time(). The former is a bona fide PHP function that grabs the current local time from the server while the latter is a WordPress function that grabs the time of publishing for the post.
It doesn’t take a genius to figure out how we’re going to use these two: Taking the current time and subtracting the published time will give you the time difference, in other words how long ago the post was published. The challenge is that you need to get the right time formats to actually output a rational result.
Getting the right kind of time
You’d think that if you want to count how many days ago a post was posted you could just grab the dates and count backwards. But that’s actually pretty hard because it relies on the counting mechanism knowing what day of the month it is and how many days each month has etc etc. A better solution is to grab a unified time value that doesn’t change in the same arbitrary way. Luckily programmers found this problem and its solution a long long time ago. It is called the the Unix Epoch and is the time, in seconds, since January 1, 1970 00:00:00 GMT). To get this value for each of our functions we simply add in the format string “U” like this:
date('U'); // outputs the current date and time in Unix Epoch terms
get_the_time('U'); // outputs the publishing date and time in Unix Epoch terms
Making the time make sense
Problem is this produces a weird long number like 6250458. This is the number of seconds since the article was published. We want the number of days, so we need to do some math.
If you have seconds and you want to make days you first have to divide them by 60 to make minutes, then divide it by 60 again to make hours and then finally divide it by 24 to make days. For 86399 out of every 86400 seconds of the day this will produce an integer (number with a comma) but we want whole numbers. So we utilize an extra function called round() that rounds out the number for us.
Putting it together
With that we have all the components we need to get the number of days since published. In PHP form the function looks like this:
<?php
$days = round((date('U') - get_the_time('U')) / (60*60*24));
echo "Published " . $days . " days ago";
?>
This will output the number of days since published sandwhiched between the words “Published” and “days ago”. But what about the first day, when the number is zero, or the second day when the number is one? “Published 0 days ago” and “Publised 1 days ago” looks kind of sloppy, no? Well, it’s easy to fix with some conditional statements:
<?php
$days = round((date('U') - get_the_time('U')) / (60*60*24));
if ($days==0) {
echo "Published today";
}
elseif ($days==1) {
echo "Published yesterday";
}
else {
echo "Published" . $days . " days ago";
}
?>
All these conditional statements do is ask “if the number of days is 0, say “Published today”, if the nuber is 1, say “Published yesterda” and for all other cases say “Published X days ago”.
Copy this piece of code anywhere in your theme files within the WordPress Loop and it’ll spit out a published x days ago timestamp on your posts.
Easy as Pi.
8 replies on “Add a Twitter-like “Published X Days Ago” timestamp to your WordPress posts”
You not have that more complex version I gave you last year? The one that did seconds / minutes / hours / days / weeks ?
this works perfectly.
your code is spewing some junk in the comment section. still new to wordpress but is there a way to suppress this info so the user does experience this.
cheers,
oliver
Warning: call_user_func(twentyten_comment) [function.call-user-func]: First argument is expected to be a valid callback in /homepages/30/d170018018/htdocs/blog/wp-includes/comment-template.php on line 1333
Warning: call_user_func(twentyten_comment) [function.call-user-func]: First argument is expected to be a valid callback in /homepages/30/d170018018/htdocs/blog/wp-includes/comment-template.php on line 1333
Thanks for spotting that. I made a slight change in the theme and it had an unexpected side effect. Solved now as you can see.
but where in the editor do we place this code ???
Actually i got a premium theme, but it got no time or date thing in it !
You place it wherever you want the info to appear. If you are using a premium theme I recommend creating a child theme and then making changes in that rather than in the core theme.
Really very very thanks,
i am google about this for last 4 days…
fortunately today i got it…
This is exactly what i am looking for…
Wow… really helpful but if some one know the plugin which shows TIME AGO using AJAX. so that without refresh page we have accurate time ago stamp data…
thanks