12% off of LTD using this coupon: TWELVEPERCENTOFF. Promo ends on 2 Dec midnight UTC.
Published on Feb 19, 2020

Display Relative Post Modified Time (eg 3 days ago) in Oxygen

David Browne

Sometimes we may need to display the time when a post was last updated in more human ‘scannable’ way. 6 months ago, 3 days ago, 5 mins ago. It can be quicker to understand than just showing a date.

WordPress has us covered here as there is a function human_time_diff(), which allows us to easily compare two seperate times and then display this in a human readable format.

We can use this function to compare the current time to the time when the post was last modified.

Using the code snippets plugin, let’s create a new function that does exactly this;

function relative_modified_time() {
    $last_modified_time = get_the_modified_time('U');
    $current_time = current_time('U');
    return human_time_diff($last_modified_time,$current_time) . " ago";  

We set this to run on the front end only.

Displaying in Oxygen for Single Posts

Now we have our function, we can use it inside Oxygen. We can add a text element, then select ‘insert data’ at the top of the page.

Next, we select ‘PHP Function Return Value’ and use our new function.

Displaying in Oxygen for Archives

If we wanted to display this data inside Easy Posts for an archive page, we would need to go to the PHP template and echo our function.

<?php echo relative_modified_time(); ?>

All done