Sauter la navigation

[Résolu] Calculate age from date field

This support ticket is created Il y a 6 années et 1 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.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

Ce sujet contient 7 réponses, a 2 voix.

Dernière mise à jour par Minesh Il y a 6 années et 1 mois.

Assisté par: Minesh.

Auteur
Publications
#1154922

I am trying to: Calculate age from date field
the code I have used into Toolset -> Settings -> Custom Code

add_shortcode( 'iam-eta', 'wpv_post_age_shortcode');
function wpv_post_age_shortcode( $atts ) {
   global $post;
 $timestamp = get_post_meta( get_the_ID(), 'data-di-nascita', true );
$age = round((time() - $timestamp) / ( 60*60*24* 365 )) ;
return $age;
}

Link to a page where the issue can be seen: lien caché

I expected to see: with the shortcode [iam-eta] I expected to see the age

Instead, I got: only the number 49

#1155306

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - if the field "data-di-nascita" is created using Types, all Types custom fields have prefix "wpcf-" when you try to access is directly using core WordPress function.

So, if field "data-di-nascita" is indeed created using Types plugin, then you need to usee the code:

add_shortcode( 'iam-eta', 'wpv_post_age_shortcode');
function wpv_post_age_shortcode( $atts ) {
   global $post;
 $timestamp = get_post_meta( $post->ID, 'wpcf-data-di-nascita', true );
$age = round((time() - $timestamp) / ( 60*60*24* 365 )) ;
return $age;
}

Could you pelase let me know if above solution help you to resolve your issue.

#1155668

My issue is resolved now. Thank you!

#1155706

So, one other question:
I'm 37 years old, my birthday is in 9 march 1981, with your code result that my age is 38.
What sould I do to visualize the correct age?

#1155995

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Sorry, you want to display exact age with Year, months, days?

#1156143

Sorry, you want to display exact age with Year, months, days?
Yes I want to do that!

Les nouveaux fils créés par Minesh et associés à celui-ci sont repris ci-dessous :

https://toolset.com/fr/forums/topic/split-calculate-age-from-date-field-show-year-months-days/

#1156159

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Well - I've split the ticket and I will answer your question in the new ticket. Could you please close this ticket and we will continue with the split ticket.

#1156218

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Could you please close this ticket as well 🙂