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
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>enlace oculto</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]
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
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.
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!
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
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!!