Home Forums General Issues Calculate Age from Date field Reply To: Calculate Age from Date field

  • This one worked for me!

        $date = get_field('date_of_birth');
        $birthday = new DateTime($date);
        $interval = $birthday->diff(new DateTime);
        echo $interval->y.' years old';

    This one:
    //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!