I'm exposing civi contact data in a drupal view. I can expose date of birth, but not the contact age field.

Anyone know of a module/ combination of modules that will allow me to calculate age in years within the view?

2 Answers 2


In your view, pull in the CiviCRM birth date field. Under date format, select Time ago. This will present the D.O.B. field as 40 years 7 months

enter image description here

To remove years and months from the output you can ‘Rewrite results’ in the view and ‘Trim this field to a maximum length’ and set ‘Maximum length’ to 2 characters


You need to add views php module

Add the dob into a view. In this example called birth_date

Add a global php field.

Then put this code into the output code box in the global php field

$ageTime = $row->birth_date;
$t = time();
$age = $t-$ageTime;
$year = 60 * 60 * 24 * 365;
$ageYears = $age / $year;
print floor($ageYears);
  • Thanks Tony. Anil's suggestion has worked a charm. So I haven't needed to dabble with PHP Mar 1, 2019 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.