Skip Navigation

[Resolved] Calculate age according to custom number field “year of birth”

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to calculate an age in years based on a given birth year.

Solution: It's not possible to calculate an age with only a year. You must have a year, month, and day to perform an accurate calculation. I have created a custom shortcode for this purpose, you can adapt for your needs.

Relevant Documentation:
https://toolset.com/forums/topic/custom-date/#post-571557

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

Our next available supporter will start replying to tickets in about 6.46 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 6 replies, has 2 voices.

Last updated by Ido Angel 5 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#1180921

hey,
I have a "year of birth" custom field for a "writer" custom post type. I was wondering if I can show the current age of the writer by calculating the years that have passed since the "year of birth" and up until now, and show that on the site. the idea is that this number would change automatically every year.
any ideas?
thanks!
ido

#1180946

There's nothing built-in to Toolset, but I've worked on another ticket recently that did something similar. I made a custom shortcode, and with a few adjustments and I think you'll be able to use it. This code requires PHP 5.3.0+.

Add this to your child theme's functions.php file or to a new code snippet in Toolset > Settings > Custom code:

// format-date-difference
// Custom shortcode that displays a formatted string showing the difference between two dates
// @start - Unix timestamp, default is current timestamp
// @end - Unix timestamp, default is current timestamp
// @format - DateInterval format, default is '%y years': see <em><u>hidden link</u></em>
// example: [format-date-difference start="[types field='year-of-birth' output='raw'][/types]" format="You are %y years old"][/format-date-difference]
add_shortcode( 'format-date-difference', 'format_date_difference_func');
function format_date_difference_func($atts = [])
{
  $atts = shortcode_atts([
    'start' => date('U'),
    'end' => date('U'),
    'format' => '%y years, %m months, %d days',
  ], $atts);
  if( trim($atts['start']) == '' || trim($atts['end']) == '')
    return;
  $datetime1 = new DateTime();
  $datetime1->setTimestamp($atts['start']);
  $datetime2 = new DateTime();
  $datetime2->setTimestamp($atts['end']);
  $interval = $datetime1->diff($datetime2);
  return $interval->format($atts['format']);
}

You can change the default format if you'd like, but you can also customize that in the shortcode. You will set the start date to be the raw value of the custom field. You can omit the end date, since the default is the current timestamp. Example:

[format-date-difference start="[types field='year-of-birth' output='raw'][/types]" format="You are %y years old"][/format-date-difference]
#1180953

Hey Christian!

Nice 🙂 Thanks!
Doesn't work for me, as my "year-of-birth" field is just a number - the number of the year. It's this way because I'm importing approx. 1500 writers into the site and I'm using wp all import (not sure it supports date field imports).
Is there any adjustment I can make in your code to make this work?
Thanks!
Ido

#1180993

Doesn't work for me, as my "year-of-birth" field is just a number - the number of the year
But what if the User's birthdate is December 1, 2009. That person is only 9 years old because they have not had a birthday yet this year. But if I calculate their age based on the year alone, it comes to 10 (2019 - 2009). It's wrong.

#1180995

Good point.
So - what format will the code work with if the date is raw? 15/02/2019? Or would I have to make the field an actual date field?
Thx!

#1181003

The code here won't work with anything but a Unix timestamp. I wrote a slightly different version that works with a different date format like "1994-01-01". If you need something else, you will probably have to play around with PHP DateTime to get exactly what you want, or convert the dates you have to a usable format.
https://toolset.com/forums/topic/custom-date/#post-571557

#1181044

Great, I found a way to import the date (i enter it like this: "October 8, 1974") - and this way the code works!
Many thanks!!