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 6.x

This version works for me in Drupal 6:

<?php
$year_diff = date("Y") - ($account->profile_birthdate['year']);
$month_diff = date("m") - ($account->profile_birthdate['month']);
$day_diff = date("d") - ($account->profile_birthdate['day']);
if ($month_diff < 0)
  $year_diff--;
elseif ($month_diff == 0 && $day_diff < 0)
  $year_diff--;

print 'Age: ' . $year_diff;
?>

('profile_birthdate' stands for the 'Date of birth' field. Change it according to your configuration.)

FIXED Drupal 6

The above code "works" in a way, but is quite glitchy and doesn't always return the correct age. I found a fix and put it into a nice function which I added below. You can see this fix in action at http://www.coldcast.org/profile/darklight

<?php
function userage($uid) {
$data = user_load($uid);
$year_diff  = date("Y") - $data->profile_birthdate{year};
$month_diff = date("m") - $data->profile_birthdate{month};
$day_diff   = date("d") - $data->profile_birthdate{day};
if($month_diff < 0) $year_diff--;
elseif(($month_diff==0) && ($day_diff < 0)) $year_diff--;
return $year_diff;
}
?>

As in the original code, be sure to change "profile_birthdate" to match your own setup.

Drupal 5.x

For me there should be

<?php
global $user;
?>

Comments

fabrizioprocopio’s picture

To use this useful snippet with -content profile- module
where it has to be pasted?
always in user_profile.tpl.php file ???
or else can I use a regular cck text field (not filtred) to show the user age in the node type set as user profile?

prashant7717’s picture

I am new to drual
could you please let me know how to display age value inside content?