Calculate Difference in Years Between Two Dates using a shortcode

Sometimes you need to calculate a person's age on the fly, but on a given date, not today's.

This is especially useful in Formidable Pro Views, where you can use Formidable shortcodes to populate the date variables.

 * function calculate_years_diff($atts = array( 'birthdate' => 'Y-m-d','effective' => 'Y-m-d')
 * calculates difference in years between two dates
 * can be called by shortcode 
function calculate_years_diff($atts) {
	$defaults = array(
		'birthdate' => false,
		'effective' => false
	$atts = shortcode_atts($defaults, $atts, 'calculate_age');
	if( false === $birthdate || false === $effective ) {
	} else {
		$birthdate 	= new DateTime( $birthdate );
		$effective 	= new DateTime( $effective );	
		$interval 	= $birthdate->diff($effective);
		$age 		= $interval->y;
	return $age;

Leave a Reply

Your email address will not be published. Required fields are marked *

I accept the Privacy Policy

Recent Posts

October 5, 2020
Easy Ways To Automate Your Website - 2 approaches

Isn't technology supposed to make life easier? I recently built a tool for a provider of an ERP system, to allow his clients to automatically publish inventory to their business websites and keep the sites in synch with his system. It got me thinking (again) about automation, and how business can do more with tools they're already using.

June 17, 2020
The Pivot: Making the shift from Commodity to Consultant

A lot of people - even companies - that provide technical services find themselves in the unfortunate position of being viewed as "the techs" and ignored when discussion turns to policy, marketing, or other topics relating to business management and strategy - the domain of consultants. This can be the case whether the tech is a twenty-something fresh out of school, or someone who's been in the industry for decades.
It's difficult to change people's perception once you've been slotted into a role. Far better to set expectations at the beginning of a relationship than try to change mid-stride.

June 4, 2020
An office without walls. A business without borders. There's no need to meet in person.

I should have known better, but I was hit with a wave of panic when the government announced the beginning of the corona virus lock down. I thought that might be it for my business. How would I get new clients? What about the meetings I had already scheduled? What if everybody stopped spending? Even in a digital business it's sometimes difficult to see past traditional expectations.

To my eternal relief, I learned better.

May 29, 2020
Your Website is an Investment, NOT an Expense. Demand ROI

Viewing your website(s) as a necessary expense rather than as an investment that's expected to produce a return is a critical misstep. Business owners often have a difficult time defining the role of their Website in their online presence and marketing. People often confess to feeling overwhelmed by technology, and rather than deal with the complexity, they settle for a site that's less functional than a business card or a billboard.

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

Email sending image

You have Successfully Subscribed!