0
Days
0
Hours
0
Minutes
0
Seconds

Black Friday Sale - Get 30% OFF. Grab our only sale of the year!

Use discount code BF2024 at checkout.

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

Profile Builder Pro

Create beautiful front-end registration and profile forms with custom fields. Setup member directories, custom redirects, cutomize user emails & more using the all in one user management plugin.

Get Profile Builder

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