I need help with a computed field that returns a user's age given the date of birth from Date 2.0 field.

I created the field dod (field_dob)

It's a select list
input format 2 sep 2008
years back and forth: -90: -16 (as I want my site open to 16+)
yime increment 1
granulity: year, month, day

I then created a computed field: field_age

I have this code

$birthday_date = date_make_date($node->field__[0]['value']);
$birthday = $birthday_date->db->parts;

//compute age
$bdayunix = mktime("", "", "", $birthday['mon'], $birthday['mday'], $birthday['year']);
$nowunix = time();
$unixage = $nowunix - $bdayunix;
$age = floor($unixage/ (365 * 24 * 60 * 60));

$node_field[0]['value'] = $age;

BUt I don't know what to put in the following:

Data Type
Data Length
Default Value

Thank you

Comments

Justin Freeman’s picture

My guess is that:

$birthday_date = date_make_date($node->field__[0]['value']);

Should read:

$birthday_date = date_make_date($node->field_dob[0]['value']);

Or was that just a typo in your posting?

Casper_Deseyne’s picture

Data Type = Integer
Data Length = 2
Default Value = 0

droople’s picture

The age is showing up wrongly.

eg: a dob of 1 January 1990is giving age: 38

(actually all profiles are showing age 38)

I am getting this error

warning: mktime() expects parameter 1 to be long, string given in /home/canhost/public_html/direct/sites/all/modules/computed_field/computed_field.module(118) : eval()'d code on line 5.

with these settings

$birthday_date = date_make_date($node->field_dob[0]['value']);
$birthday = $birthday_date->db->parts;

//compute age
$bdayunix = mktime("", "", "", $birthday['mon'], $birthday['mday'], $birthday['year']);
$nowunix = time();
$unixage = $nowunix - $bdayunix;
$age = floor($unixage/ (365 * 24 * 60 * 60));

$node_field[0]['value'] = $age;

Data Type = Integer
Data Length = 2
Default Value = 0

Thank you

altparty’s picture

I experience the same problem: all profiles show the age of 38. Does anyone have a solution available?

droople’s picture

bump

rightchoice2c_me’s picture

Work hard to understand what you have to do before you start you may not be able to develop every detail but the more you know the less risk you take.

Now coming to you guys problem, if you get any errors please try to have a close look at that and check whether it returns the expected values appropriately or not.


warning: mktime() expects parameter 1 to be long, string given in /home/canhost/public_html/direct/sites/all/modules/computed_field/computed_field.module(118) : eval()'d code on line 5.

here is the solution, try the below code and hope this solves your problem.
$date = $node->field_age[0]['value'];
list($year, $month, $day) = split('[/.-]', $date);

$bdayunix = mktime(0,0,0, $month, $day, $year);
$nowunix = time();
$unixage = $nowunix - $bdayunix;
$age = floor($unixage/ (365 * 24 * 60 * 60));

$node_field[0]['value'] = $age;

kreativalab’s picture

try to use compound field, the problem is that you can show the age but you can't store the data in your database, so this mean you can't use any views to search your data....

how i can search my users by age range...should i hook the birtdhday module or the profile core module...any idea?

ciaooooo

sivaji_ganesh_jojodae’s picture

Version: 5.x-1.2 » 6.x-1.0-beta2

I am successfully able to get age from date of birth based on this tutorial http://drupalsn.com/learn-drupal/drupal-tutorials/user-age-date-birth

I guess the age is calculate from date of birth field value based on the some code logic which is invoked only on creating / editing a node. The computed age in stored in some cck module table and they *never gets increment automatically over a period of time*.

If my guess is correct, please let me know the way to keep the age field upto date.

foodbo’s picture

yup, i have done that using the tutorial - http://drupalsn.com/learn-drupal/drupal-tutorials/user-age-date-birth-%E...,

As the age is computed and does not store in the database, so How can I search the the age? e.g. age 25 - 40, 40 - 45....

thx

mmachina’s picture

i just can't seem to get it to work... anyone else have similar problems?

eL’s picture

I am courious too,

no prob to get age by computed field. But - is this value recomputed? Is age changing? How storing values in database could fluence this?

nopu’s picture

Issue summary: View changes

Guys all these code is not working can any one please let me know for drupal7 site how to calculate the age for dob field.

silvi.addweb’s picture

For solving this issue add this code into Computed Code (PHP) box field setting.

$current_date = date('m/d/Y');  // change as per your date format.
$date = $entity->field_date['und'][0]['value'];
$age =  date_create($date)->diff(date_create($current_date))->y;
$entity_field[0]['value'] = $age;

Let me know if you face any query/concern regarding this.

Thanks!

dqd’s picture

Status: Active » Closed (outdated)

Due to the Drupal core life cycle policy and security support advisery, Drupal 6 is no longer supported. So issues for Drupal 6 cannot be longer maintained. The project maintainer has asked for closing all D6 issues to clean up the issue queue. Feel free to reopen the issue if required or set it to "needs to be ported" and latest D8 dev version, if the issue discusses a still missing feature which can be implemented in the D8 version.