Content Marketing

Stop Updating Years On Your WordPress Site With This Shortcode To Calculate Years Since

One of the greatest features of WordPress is Shortcodes. The flexibility to program dynamic content using shortcodes can help you to deploy robust and incredible sites. Shortcodes are basically substitution strings that you can insert into your content that renders dynamic content.

I’m helping a client this week where they’re taking one of their products and rolling it out into a new domain. The site is hundreds of pages and has been quite an undertaking. As we’ve been working on the hit list of issues, one that popped up was that there were dozens of blog posts, pages, and calls to action that spoke to the company’s years in business.

Some pages had 13, some 15, others were accurate at 17… all depending upon when they were written. This is one of those unnecessary edits to need to make that a shortcode can handle perfectly.

Shortcode for Years Since

All we need to do is register a shortcode that takes the current year and subtracts it from the year the company was established. We can place a default year to always calculate from OR we can pass the year. We can register the shortcode by adding this function to the site’s theme’s functions.php file.

If you’re advanced, you may want to build a custom plugin for your site so that these shortcodes still work even if you update to a new theme:

function yearssince_shortcode($atts) {
	$atts = shortcode_atts(array(
		'startdate' => '7/14/2005',
	$startdate = new DateTime($atts['startdate']);
	$today = new DateTime(date('m/d/Y'));
	$datediff = $today->diff($startdate);
	$yeardiff = $datediff->y;
	return $yeardiff;
 add_shortcode( 'yearssince', 'yearssince_shortcode' );

What the function does is subtract the current year from the year specified, or use the date you enter in this code as the default. In this case, I used the date of the first published post on Martech Zone.

This shortcode will calculate the number of years since that date. As an example, if I wish to write how long Martech Zone has been published, I just write:

Martech Zone has been published for over [yearssince] years!

The result is:

Martech Zone has been published for over 18 years!

Of course, you can get far more complex with this type of shortcode… you could use HTML, images, CSS, etc. but this is just a simple example just to make sure your site is already accurate!

Disclosure: Martech Zone is an affiliate for WordPress and is using an affiliate link in this article.

Douglas Karr

Douglas Karr is the founder of the Martech Zone and CEO of DK New Media. Douglas has helped dozens of successful MarTech startups, has assisted in the due diligence of over $5 bil in Martech acquisitions and investments, and continues to assist companies in implementing and automating their sales and marketing strategies. Douglas is an internationally recognized digital transformation and MarTech expert and speaker. Douglas is also a published author of a Dummie's guide and a business leadership book.

Related Articles


Adblock Detected

Martech Zone is able to provide you this content at no cost because we monetize our site through ad revenue, affiliate links, and sponsorships. We would appreciate if you would remove your ad blocker as you view our site.