In this tutorial, we’ll follow on from a previous tutorial Displaying the Relative Post Modified Time in Oxygen. This time we’ll look at how to display the relative published date & then how we can modify the output conditionally based on that relative time.
In our WPDD facebook group, a user wanted to be able to display the post publish dated differently depending on how long it had been since the posts were published.
The specific requirement is to be able to display ‘Added more than a week ago’ to any posts that were published more than a week ago, even if they were published a few months or even years ago.
It will need to display ‘Added x days ago’ up until the post is at least one week old, then display ‘Added more than a week ago’ from then on.
First though, we’ll go through the more common use case, which is displaying the relative modified date in number of days, months and years. Then we’ll look at how we can modify it afterwards for this specific use case.
Displaying the Relative Published Date in Oxygen
The typical use case would be needing to create a function that displays;
- x days ago
- x weeks ago
- x months ago
- x years ago
This can then be used this inside a text element inside a sentence; ‘Added x days ago’ or ‘Published more than x days ago’ or however we wish to word it.
Like in the relative modified time tutorial, we’ll be using the human_time_diff() function to compare two times. The post published time and the current time.
Let’s create our new function in our code snippets to get our relative published time.
function relative_published_time() {
$published_time = get_the_time('U');
$current_time = current_time('U');
return human_time_diff($published_time,$current_time) . " ago";
}
We can display this in Oxygen in our single post template, or inside a repeater by inserting a text element, selecting ‘insert data’ at the top of the page, going to PHP Function Return Value and then adding in our new function name.
To display this inside an Easy Posts instead, we would need to edit the PHP template and echo our function like so;
<?php echo relative_published_time(); ?>
Filtering to change the output
For the more specific use case that was asked for (wanting to display ‘more than a week ago’ after a post reaches one week old and thereafter) we need to filter the output of the human_time_diff() function that we’ve been using.
We still want the human_time_diff() function to do all the hard work for us, comparing the times and giving us the human readable format. We just need to make sure that when a post is over a week old, the function doesn’t output it’s usual thing, it outputs instead what we tell it to.
Here is the code we can add to our code snippets to do exactly that;
add_filter( 'human_time_diff', 'lit_time_diff', 10, 4 );
function lit_time_diff( $since, $diff ){
if ( $diff >= WEEK_IN_SECONDS ) { // If over a week
$since = 'more than a week'; // Our custom text to output
}
return $since;
}
Here we’re making it output ‘more than a week’ for posts over a week old. Posts less than a week old won’t be effected and the output will remain as it was before;
- Added ‘3 hours’ ago
- Added ‘5 days’ ago
- Added ‘more than a week’ ago
More than a year ago
It might be that we once posts become over a year old, having ‘more than a week’ doesn’t seem relevant anymore. If this is the case, we could display ‘more than a year’ from that point onwards.
Here is an example of the amended code with a new conditional, including when posts are over a year old.
add_filter( 'human_time_diff', 'lit_time_diff', 10, 4 );
function lit_time_diff( $since, $diff ){
if ( $diff < YEAR_IN_SECONDS && $diff >= WEEK_IN_SECONDS ) { // If over a week, but less than a year
$since = 'more than a week'; // Our custom text to output
}
elseif ( $diff >= YEAR_IN_SECONDS ) { // Anytime over a year
$since = 'more than a year'; // Our custom text to output
}
return $since;
}