Skip Navigation

[Resolved] Display Difference between 2 dates fields

This support ticket is created 7 years, 6 months 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
- 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)

This topic contains 1 reply, has 2 voices.

Last updated by Minesh 7 years, 6 months ago.

Assisted by: Minesh.

Author
Posts
#435373

I have two custom fields
Year Born and Year Died

I would automatically display Age which would be Year Died - Year Born.
Is there an easy way to do this?

Thanks

#435587

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

To calculate the age using custom field you need to write custom shortcode:

Add following code to your current theme's functions.php file:

add_shortcode( 'calculate-age', 'wpv_post_age_shortcode');
function wpv_post_age_shortcode( $atts ) {
   global $post;
 
   $year_died = get_post_meta( $post->ID, 'wpcf-year_died', true );
   $year_born = get_post_meta( $post->ID, 'wpcf-year_born', true );
   $age = $year_died - $year_born;
   return round( $age / ( 86400 * 365 ) );
}

Where:
Replace 'year_died' and 'year_born' with your original field name.

I hope above solution will help you to resolve your issue.

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