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?
CiviCRM Stack Exchange is a question and answer site for administrators and users of the CiviCRM Constituent Relationship Management software. It only takes a minute to sign up.
Sign up to join this communityI'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?
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
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
<?php
$ageTime = $row->birth_date;
$t = time();
$age = $t-$ageTime;
$year = 60 * 60 * 24 * 365;
$ageYears = $age / $year;
print floor($ageYears);
?>