A Cozmoslabs Product

Current Age shortcode

This function adds a shortcode that displays current age based on birthdate from datepicker.

Shortcode:

[age date=”mm/dd/yyyy”]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
 * Current Age shortcode based on birthdate from datepicker. Tags: age, shortcode, datepicker
 * Use like [age date="mm/dd/yyyy"]
 */
add_shortcode( 'age', 'wppbc_calculate_age' );
function wppbc_calculate_age( $atts, $content ){
	$atts = shortcode_atts(	array(
			'date' => '',
			'format' => 'm/d/Y'
		), $atts );
 
	if ( wppbc_validateDate( $atts['date'], $atts['format'] ) ){
		$formated_date = DateTime::createFromFormat($atts['format'], $atts['date']);
		$to   = new DateTime('today');
		return $formated_date->diff($to)->y;
	}
 
	return $atts['date'];
}
function wppbc_validateDate($date, $format = 'Y-m-d H:i:s')
{
	$d = DateTime::createFromFormat($format, $date);
	return $d && $d->format($format) == $date;
}

The Ultimate Membership Bundle

Combine the power of Profile Builder with Paid Member Subscriptions to set up user registration, memberships, and recurring revenue.

Get 25% off with the bundle

Help & Support

We’re here to help you every step of the way.

Open a Support Ticket