Skip Navigation

[Résolu] Get birth date year using php and calculate current age

This support ticket is created Il y a 6 années et 6 mois. 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
- 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 réponses, has 2 voix.

Last updated by davidZ-4 Il y a 6 années et 6 mois.

Assisted by: Luo Yang.

Auteur
Publications
#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

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.