Skip Navigation

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

This support ticket is created hace 7 años, 1 mes. 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.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

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)

Este tema contiene 2 respuestas, tiene 2 mensajes.

Última actualización por davidZ-4 hace 7 años, 1 mes.

Asistido por: Luo Yang.

Autor
Mensajes
#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?
enlace oculto

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