Home › Forums › General Issues › Calculate Age from Date field › Reply To: Calculate Age from Date field
This one worked for me!
<?php
$date = get_field('date_of_birth');
$birthday = new DateTime($date);
$interval = $birthday->diff(new DateTime);
echo $interval->y.' years old';
?>
This one:
<?php
//date in mm/dd/yyyy format; or it can be in other formats as well
$birthDate = get_field('date_of_birth');
//explode the date to get month, day and year
$birthDate = explode("/", $birthDate);
//get age from date or birthdate
$age = (date("md", date("U", mktime(0, 0, 0, $birthDate[0], $birthDate[1], $birthDate[2]))) > date("md")
? ((date("Y") - $birthDate[2]))
: (date("Y") - $birthDate[2]));
echo "Age:" . $age;
?>
For some reason generated the current year. But no matter, I have it all working now, thanks!
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.