Skip Navigation

[Resolved] Calculate age based on Date of Birth field

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to display a User's age based on a custom date field that stores their birthdate.

Solution: Use the following custom shortcode:

add_shortcode( 'time_ago', 'time_ago_func');
function time_ago_func($atts){
    global $wpdb;
    extract( shortcode_atts( array(
    'birthdate' => '',
    ), $atts ) );
 // handle the case where birthdate field is not set
     if( !$birthdate ) {
       return '';
     }
      
$age = floor((time() - $birthdate)/(365*24*60*60));
     return $age;
}

Use the following syntax to display the User's age:

[time_ago birthdate='[types usermeta="nanny-date-of-birth" raw="true" user_current="true"][/types]'] years old
This support ticket is created 5 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 5 replies, has 2 voices.

Last updated by simonM-5 5 years ago.

Assisted by: Christian Cox.

Author
Posts
#1367521

Tell us what you are trying to do?
We have a Custom User Field "Date of Birth".

We would like to be able to display a field containing just the age in years, based on the calculated age from the Date of Birth field.

Is there any documentation that you are following?
https://toolset.com/forums/topic/calculatedisplay-current-age-based-on-date-field/
However, I was not 100% sure what is meant here or where exactly this function should be called. Does this need to be added as Custom Code snippet, for example?

What is the link to your site?
hidden link

#1367791

Hi, I assume you have a custom date field created that stores a birthdate, correct? If so, I would follow this example:
https://toolset.com/forums/topic/calculate-age-from-date-of-birth-custom-filed/#post-164510
The PHP code can be added to a new custom code snippet in Toolset > Settings > Custom Code, or you can add it to a child theme's functions.php file.
Let me know if you have additional questions about that.

#1367907

Hi Christian

- I added the final piece of code to Toolset > Settings > Custom Code.

----------
add_shortcode( 'time_ago', 'time_ago_func');
function time_ago_func($atts){
global $wpdb;
extract( shortcode_atts( array(
'birthdate' => '',
), $atts ) );

//explode the date to get month, day and year
//$birthDate = explode("-", $birthDate);
//get age from date or birthdate
//$age = (date("md", date("U", mktime(0, 0, 0, $birthDate[0], $birthDate[1], $birthDate[2]))) > date("md") ? ((date("y")-$birthDate[2])-1):(date("Y")-$birthDate[2]));
$age = floor((time() - $birthdate)/(365*24*60*60));
return $age;
}
---------

- On the Account page, I added the following to display the user's age on their own Account page.

[time_ago birthdate='[types field="data-di-nascita" raw="true" id=""][/types]'] years old

However
1) I am also seeing the following error message at the top of the page:

Warning: A non-numeric value encountered in /var/www/kle1116-204/apps/dev/wp-content/toolset-customizations/calculate-nanny-age-code-snippet.php on line 21

2) The age is not correct (it is always 49, regardless of what date of birth the Native Nanny sets).

If you want to reproduce the error on our site:
1) create yourself as a Native Nanny (click the Become a Native Nanny link)
2) Log in
3) Once logged in, you should see the error immediately, as you land on the Account page automatically.

Let me know if you need WP Admin access (which you should already have from our other open ticket with you regarding Email Notifications on submission of CRED forms)

Best regards
Simon

#1367967

Hi, you probably need to update the field slug here:

[time_ago birthdate='[types field="data-di-nascita" raw="true" id=""][/types]'] years old

Replace data-di-nascita with your date field slug. If that doesn't solve the problem, please provide login credentials for wp-admin in the private reply fields here.

#1367997

The main thing I see here is that the birthdate field is not a post field, but a user field. The syntax for accessing User fields is a bit different:

[time_ago birthdate='[types usermeta="nanny-date-of-birth" raw="true" user_current="true"][/types]'] years old

I made that adjustment, as well as another minor update that will prevent that warning you see when no birthdate is supplied. Take a look now.

add_shortcode( 'time_ago', 'time_ago_func');
function time_ago_func($atts){
    global $wpdb;
    extract( shortcode_atts( array(
    'birthdate' => '',
    ), $atts ) );
 // handle the case where birthdate field is not set
     if( !$birthdate ) {
       return '';
     }
     
$age = floor((time() - $birthdate)/(365*24*60*60));
     return $age;
}
#1368001

I wasn't aware user fields had to be addressed differently from post fields.

That appears to work like a charm. Thank you!

My issue is resolved now.