Display users age based on a date-of-birth field

PLEASE NOTE! These snippets are user submitted. It is impossible to check them all, so please use at your own risk! For users who have setup drupal using an alternate database to the default (MYSQL), please note that the snippets may contain some database queries specific to MYSQL.

Description

This php snippet displays the age of the user, based on a date of birth field.

Dependencies: profile.module must be installed & enabled and a custom profile date field called profile_dob must be setup. This is the DATE OF BIRTH field that is used to dynamically determine the users age.

Note: If you haven't setup your date-of-birth field yet, go to Administer › User management >Profiles and add a new DATE field. Give it the name profile_dob and fill out the rest of the options as suits.

Thanks to Pepe for improving the snippet.

Usage

  • For use in your user profile page override
  • Using a text editor like NOTEPAD.EXE or an equivalent, copy and paste the snippet into your user_profile.tpl.php file
  • Change the div class name or the message text to suit.

<div class="fields">
<?php
    $year_diff
= date("Y") - ($user->profile_dob{year});
   
$month_diff = date("m") - ($user->profile_dob{month});
   
$day_diff = date("d") - ($user->profile_dob{day});
    if (
$month_diff < 0)
     
$year_diff--;
    elseif (
$month_diff == 0 && $day_diff < 0)
     
$year_diff--;
?>

<p>AGE: <?php print $year_diff; ?> </p>
</div>

 
 

Drupal is a registered trademark of Dries Buytaert.