Home › Forums › Front-end Issues › Display age from birth date
Hello,
I try to set up the automatic display of a person’s age, same way as Wikipedia does.
exemple : 17 mai 1990 (33 ans)
here is my code:
<?php echo get_field( 'person-birthday', $post->ID ); ?> //display the date
<?php $date_string = get_field( 'person-birthday', $post->ID );
$date = DateTime::createFromFormat( 'Ymd', $date_string );
$birthday = new DateTime( $date );
$interval = $birthday->diff( new DateTime );
echo '(' .$interval->y.' ans)'; ?>
Source: https://support.advancedcustomfields.com/forums/topic/calculate-age-from-date-field/#post-45149
My issue is that the age always display “0”. Is there something wrong in my code ?

when you use get_field() it will return the date in the format you specified for “Return Format”, make sure this matches with the format you are trying to use when creating the DateTime object.

<?php
// Get the raw field value (ACF date field usually stored as Ymd, e.g. 19901231)
$date_string = get_field( 'person-birthday', $post->ID );
if( $date_string ) {
// Convert to DateTime object
$birthday = DateTime::createFromFormat( 'Ymd', $date_string );
if( $birthday ) {
// Calculate age
$interval = $birthday->diff( new DateTime() );
// Display formatted date + age
echo $birthday->format('d M Y') . ' (' . $interval->y . ' ans)';
}
}
?>
Hi jnrmy,
You’re very close! The issue is in your DateTime::createFromFormat() call. You’re using the format ‘Ymd’ which expects a string like “19900517”, but ACF date fields by default return the date as “Ymd” format – which is exactly what you’re getting from get_field().
The problem is you’re then trying to create a new DateTime object from the result, but $date is already a DateTime object. Here’s the corrected code:
php
<?php
$date_string = get_field(‘person-birthday’, $post->ID);
if($date_string) {
$birthday = DateTime::createFromFormat(‘Ymd’, $date_string);
$today = new DateTime();
$interval = $birthday->diff($today);
// Display the original date in your preferred format
echo $birthday->format(‘d M Y’); // Shows “17 May 1990”
// Display the age
echo ‘ (‘ . $interval->y . ‘ ans)’;
}
?>
Key fixes:
Removed the redundant new DateTime($date) – $date is already a DateTime object
Added a check to ensure the date field isn’t empty
Used $birthday->format() to display the date in a readable format
Created $today as a separate DateTime object for the comparison
Alternative simpler approach:
If you prefer a more concise version:
php
<?php
$birthday = get_field(‘person-birthday’, $post->ID);
if($birthday) {
$age = date_diff(date_create($birthday), date_create(‘today’))->y;
echo date(‘d M Y’, strtotime($birthday)) . ‘ (‘ . $age . ‘ ans)’;
}
?>
The main issue was creating a DateTime object from another DateTime object, which was resetting the date. Try the first solution and it should display the correct age!
Let me know if you’re still seeing “0” after this fix.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.