Change Date from Last Updated to Relative Time in Carbonate

by Josh Koop | Last Updated:  March 26, 2021
Apply to Ezoic Surfer SEO 7 Day Trial Brave Browser
As an Amazon Associate we earn from qualifying purchases made on our website. If you make a purchase through links from this website, we may get a small share of the sale from Amazon and other similar affiliate programs. You can read our complete disclaimer information for more details.

As many will note, inside Carbonate the default display under a single post is the date of the post being updated, this dated aspect can make it easy to tell how aged a post is by any visitor which could lead to a bounce if it appears aged.

Instead I have swapped this date using a custom function to show the relative time since last update which will hopefully be a year or less if you are properly attacking yearly content audits and cleaning up frequently.

So where the theme before showed ” Last Updated: July 2020 ” it now would say ” Last Updated: 6 Months Ago ” this obfuscates the timeline to people as they don’t see this the same way.

So how do we go about making this change to the theme? Well it is actually quite simple as you will only need to add a section of code to the functions.php and then update the single line of code in your single.php file.

First lets grab the function needed to convert the time over to a relative time:

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";  
}

Copy the above and you want to add into your functions.php file just before the end of the file which looks like the below, normally the very last line in the editor:

?>

This code basically uses the built in WordPress functionality to convert time from 2 timestamps and return the difference in a human timescale instead of a fixed date. Read More on human_time_diff

Make sure to save the file after this change

So now that this will return a human time you need to actually implement it within your template for posts to display this new function instead of the previous function.

Now you will want to change the single.php to add this new function in place of the old, I have described this process below to show you what it looks like by default and what you want it to look like after.

From the basic theme setup you have php showing the_date() function, instead you will remove that part and replace it with the new function you built above relative_modified_time().

<strong>Last Updated:</strong> <?php the_date(); ?>

After this change your line should look like the below:

<strong>Last Updated: </strong> <?php echo relative_modified_time(); ?>

Make sure to save the file after this change

Now you would want to flush any cache you have, whether local, server, CDN, or inside Ezoic or similar services to load this new code for users who aren’t signed in.

If you find this helpful please feel free to donate spare change (if you can) over here.

I love technology, I have plenty of technical side experience but never really thought of blogging as a way to earn an income. With everything that has gone on in the last couple years and the instability of the job markets I started to learn how to blog and build affiliate niche sites to add more income channels to give me the chance to break free from the financial chains that bind me and find real freedom!