November 10, 2024
User age from date of birth – the CCK way!

In this tutorial, submitted by Ms_Kristina, we will be using CCK to determine age from a birthday using date, and computed field. This would work well with node-profile (5.x), and content profile (6.x). I am personally using content profile along with the 6.x version of Drupal.

First off you need the modules listed below:
Computed Field – http://drupal.org/project/computed_field
Date Field – http://drupal.org/project/date
CCK – http://drupal.org/project/cck

I am assuming you know how to install CCK, and that it is installed at this point.

1. Date module installation

Download the tar ball from the link I provided above.

Extract tar ball into the modules folder for your drupal install & upload to your server.

Go to Administer > Site Building > Modules

You will see a list of modules, a new group of modules called “Date/Time” will be available to select.

Date installation

Select “Date”, “Date API”, and “Date Timezone” — then scroll down and save configuration.

Go to Administer and you will see a list of tasks – look for “Date & Time” which you will find under the Site configuration heading.

Date configuration

Select your default timezone, select whether or not you want user-configurable timezones, and what day you want to be the first day of the week –- then scroll down and save configuration.

2. Creating birthday/date of birth field

User age from date of birth – the CCK way!

Go to Administer, under the heading “Content Management” select “Content Types”.

Choose the content type you’re adding the field to and click “Manage Fields” – in my case it was to the Profile node type.

Scroll down and you will see an area to add a new field.

Date field

Enter in the information below:

Label: Birthday
Field: dob
Select Field Type: Date
Widget: Select List
Hit save.

On the next screen enter the following information:

Default Value: select what you want.
Default Value from to Date: select what you want.
Custom Input Format: ‘F j, Y’
Years back and forward: -100:+10
Time increment: 1
Customize Date Parts: Blank
Required: Checked off
Number of values: 1
To Date: Never
Granularity: Year, Month, and Day
Default Display: Medium
Time zone handling: Site’s time zone
Hit Save.

Your birthday field has been created!

3. Installing the Computed Field module

Download the tar ball from the link I provided above.

Extract tar ball into the modules folder for your drupal install & upload to your server.

Go to Administer > Site Building > Modules

You will see a list of modules, a group of modules called “CCK” will be available.

Select “Computed Field” — then scroll down and save configuration.

4. Creating an age field

Go to Administer, under the heading “Content Management” select “Content Types”.

Choose the content type you’re adding the field to and click “Manage Fields” – in my case it was to the Profile node type.

Scroll down and you will see an area to add a new field.

Enter in the information below:

Label: Age
Field: age
Type of Data to Store: Computed
Form element to edit the data: Computed
Hit save.

Computed field

On the next screen enter the following:

Label: Age
Widget Type: Computed
Help text: Blank
Default Value PHP Code: Blank
Required: not checked off
Number of values: 1

Computed Code:

<?php
if (!$node->nid) node_save($node);
$dob = $node->field_bday<sup class="footnote"><a href="#fn18973182784e523a7b32202">0</a></sup>['value'];
$now = time();
$then = strtotime($dob);
$diff = date('Y', $now) – date('Y', $then);

if($diff < 0) /* ideally you want to prevent this from happening */
echo ‘??? – negative age.’;

if(($diff > 0) && (date(‘z’,$now) < date(‘z’,$then)))
$diff —;
$node_field<sup class=”footnote”><a href=”#fn18973182784e523a7b32202″>0</a></sup>[‘value’] = $diff;
?>

Display this field: checked off.
Display Format:

<?php
$display = $node_field_item['value'];
?>

Store using database settings below: checked off
Data type: int
Data length: 3
Default value: blank
Not NULL: checked off
Sortable: checked off

Hit Save.

Your age field has been created!

If you edit your birthday on a given node, and then view it the age should come up accurately. You may also want to go to your Content Profile node type “Display Fields” and alter how the fields are displayed.

It’s only a quick tutorial but it should help you get up and running quickly.