
After the URLs as sentences tutorial earlier this week, I thought the topic of human readable websites would be worth touching on again. This time, we’ll look at post dates.
While it’s dead simple to include post dates inside WordPress permalinks (WordPress does this in its default post permalink structure) it’s also helpful to keep the date listed within the post.
Instead of displaying dates like this:
Posted August 19, 2010
We’re going to display dates like this:
Posted yesterday
If you want to jump straight to the functionality, you can do that. Grab the Date in a Nice Tone Plugin, and use their template tag. But this is a tutorial, right? So let’s dig in and look at how this Plugin works.
Before looking at how this Plugin works, let’s think through it conceptually. Our end goal is to display a human readable post date that uses terms like yesterday and last month. So we’re going to need to build a PHP function that will do a few things:
- Find the current time.
- Find the time of the post.
- Find the difference between the two.
- If the difference equals x, display y.
It’s actually a pretty simple process. Let’s begin stepping through the wp-date-in-a-nice-tone.php file that comes with the Plugin. Feel free to download the Plugin from the link above and follow along.
The first few lines of the function look like this:
function wp_date_in_a_nice_tone() {
$postTime = get_the_time("U");
$currentTime = time();
$timeDifference = $currentTime - $postTime;
$minInSecs = 60;
$hourInSecs = 3600;
$dayInSecs = 86400;
$monthInSecs = $dayInSecs * 31;
$yearInSecs = $dayInSecs * 366;
This whole block of code sets up the variables for the rest of the function. First we get the time the post was published, using get_the_time("U");. This is a WordPress function that, along with the parameter provided by PHP, grabs the post’s date in seconds.
Next, the variable $currentTime is set to the current time, which is determined using the PHP function time();.
With the last line of the first block of code, the $timeDifference is found by subtracting the $postTime from the $currentTime. This gives us the difference, which we can use in the rest of the function to assign certain phrases to certain lengths of time.
$minInSecs = 60;
$hourInSecs = 3600;
$dayInSecs = 86400;
$monthInSecs = $dayInSecs * 31;
$yearInSecs = $dayInSecs * 366;
This second block of PHP is simple, in that it’s assigning variables to lengths of time so that we don’t have to litter the function with so many numbers. We can’t refer to an hour as “an hour” within PHP, we have to refer to it as the number of seconds. So by creating the $hourInSecs variable, we can refer to it almost as conveniently.
// if over 3 years
if ($timeDifference > ($yearInSecs * 3)) {
$dateWithNiceTone = "quite a long while ago...";
// if over 2 years
} else if ($timeDifference > ($yearInSecs * 2)) {
$dateWithNiceTone = "over two years ago";
. . .
// if over 28 days ago
} else if ($timeDifference > ($dayinSecs * 28)) {
$dateWithNiceTone = "around a month ago";
// if equal to or more than 8 days ago
} else if ($timeDifference >= ($dayInSecs * 8)) {
$dateWithNiceTone = "in the last month";
Finally, we get into the meat of the function; and it’s actually quite straightforward. Beginning with the oldest dates (in this case 3 years, though there’s no reason you couldn’t start it sooner), the time difference is checked against what would be the equivalent in 3 years of seconds. If that is the case, it sets the $dateWithNiceTone variable to “quite a long time ago…” and if that’s not the case, it continues to the next step. A pretty simple if…else if sequence.
Where this Plugin doesn’t have an administrative interface, you can see that it wouldn’t be difficult to change the words used, or to add in your own statement based on a new scenario.
If we wanted to show if a post was added yesterday, we could add this toward the bottom:
// if equal to 24 hours or more
} else if ($timeDifference > ($hourInSecs * 24)) {
$dateWithNiceTone = "yesterday";
This is one of the statements that we added for WPCandy, except in our case we went with 18 rather than 24, because we found it to be more accurate in practice.
Finally, the Plugin echoes the variable:
echo $dateWithNiceTone;
Which is, of course, to be expected.
I’m sure if you are interested in this functionality you will grab the Plugin. After all, that’s what I did. But hopefully now you not only use it, you also understand how it works. If you wanted to, perhaps you could write the function yourself now. While this is a simple Plugin with a simple purpose, having this experience will make it easier to understand, reverse engineer, and truly understand more complicated Plugins in the future.