Get age in WordPress shortcode

Following is shortcode we put together to get age from date. Reason we developed it so that we can put accurate age in content of our sites. Here is the code that needs to be added into your theme function file…

// NDARTS.CA - Age shortcode - START ------------------------------

function get_age_shortcode( $atts, $content = null ) {
   extract( shortcode_atts( array(
      ), $atts ) );
	$date = new DateTime($content);
	$now = new DateTime();
	$interval = $now->diff($date);
	return $interval->y;
add_shortcode( 'get_age', 'get_age_shortcode' );
add_filter('widget_text', 'do_shortcode');

// NDARTS.CA - Age shortcode - END ------------------------------

To use the code, just put it into a widget, post or page where you need to display the years of age like below…
Get Age in WordPress shortcode
The date should be in YYYY-MM-DD format.

Leave a Reply

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