Answers.Body: <p>You need to add views php module</p>
  
  <p>Add the dob into a view. In this example called birth_date</p>
  
  <p>Add a global php field.</p>
  
  <p>Then put this code into the output code box in the global php field</p>
  
  <pre><code>&lt;?php 
  $ageTime = $row-&gt;birth_date;
  $t = time();
  $age = $t-$ageTime;
  $year = 60 * 60 * 24 * 365;
  $ageYears = $age / $year;
  print floor($ageYears);
  ?&gt;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-01T15:16:42.990Z
Answers.Text: Thanks Tony. 
  
  Anil's suggestion has worked a charm. So I haven't needed to dabble with PHP
Answers.UserDisplayName: Craig Almond
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-01T15:00:38.047Z
Answers.LastActivityDate: 2019-03-01T15:00:38.047Z
Answers.UserDisplayName: Tony Horrocks
Answers.Body: <p>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</p>
  
  <p><a href="https://i.sstatic.net/CPuSb.png" rel="noreferrer"><img src="https://i.sstatic.net/CPuSb.png" alt="enter image description here"></a></p>
  
  <p>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 </p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-01T15:06:17.250Z
Answers.LastActivityDate: 2019-03-02T10:16:04.133Z
Answers.LastEditDate: 2019-03-02T10:16:04.133Z
Answers.UserDisplayName: Anil
Body: <p>I'm exposing civi contact data in a drupal view. I can expose date of birth, but not the contact age field.</p>
  
  <p>Anyone know of a module/ combination of modules that will allow me to calculate age in years within the view?</p>
  
Tags: <drupal-views>
Title: Calculate Contact age in a Drupal view
ThreadText: # Calculate Contact age in a Drupal view
  
  (Asked by: username_0 on 2019-03-01)
  
  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?
  
  **Tags:** drupal-views
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2019-03-01. Score: 11 | Accepted answer
  
  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](https://i.sstatic.net/CPuSb.png)](https://i.sstatic.net/CPuSb.png)
  
  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
  
  
  ---
  
  ### Answer by username_1 on 2019-03-01. Score: 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);
  ?>
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks username_1.   username_2's suggestion has worked a charm. So I haven't needed to dabble with PHP  
  
  ---
  
  