3

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?

7

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

| improve this answer | |
1

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

<?php 
$ageTime = $row->birth_date;
$t = time();
$age = $t-$ageTime;
$year = 60 * 60 * 24 * 365;
$ageYears = $age / $year;
print floor($ageYears);
?>
| improve this answer | |
  • Thanks Tony. Anil's suggestion has worked a charm. So I haven't needed to dabble with PHP – Craig Almond Mar 1 '19 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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