Skip Navigation

[Resolved] Get birth date year using php and calculate current age

This support ticket is created 7 years, 1 month 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 2 replies, has 2 voices.

Last updated by davidZ-4 7 years, 1 month ago.

Assisted by: Luo Yang.

Author
Posts
#577782

Tell us what you are trying to do? get the year of a birth date and calculate current age base on today date

Is there any documentation that you are following? https://toolset.com/documentation/customizing-sites-using-php/functions/#date

Is there a similar example that we can see? https://toolset.com/forums/topic/calculate-age-from-date-of-birth-custom-filed/

What is the link to your site?
hidden link

I'm trying to get the birth date first using the code:

$user_id = THE USER ID COMES HERE;
echo (types_render_usermeta('wpcf-birth-date', array('user_id' =>$user_id , "style" => "calendar")));

I get nothing.

my scenario is as follow
user insert BD on his profile (user custom field)
I pull the user birth date year in the format YYYY i.e. 1989
than get today date
and calculate the age into a custom field

Please advise,
thanks,

David

#577788

Dear David,

I assume the custom field "wpcf-birth-date" is a custom date field created with Types plugin, when you use function types_render_field(), you do not need to set the "wpcf-" in the field slug, for example, you can try this:

$user_id = THE USER ID COMES HERE;
$birthdate = types_render_usermeta('wpcf-birth-date', array('user_id' =>$user_id , "output" => "raw")); // the custom field value
$age = floor((time() - $birthdate)/(365*24*60*60));
#577810

Thanks Luo,

yes, the "wpcf-birth-date" is a custom date field created with Types

your code still had the "wpcf" in it and it gave me some issue but once I removed it, it works 🙂

Thanks,
for the quick help.

David