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
Comment #1
Justin Freeman commentedMy guess is that:
Should read:
Or was that just a typo in your posting?
Comment #2
Casper_Deseyne commentedData Type = Integer
Data Length = 2
Default Value = 0
Comment #3
droople commentedThe 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
with these settings
Data Type = Integer
Data Length = 2
Default Value = 0
Thank you
Comment #4
altparty commentedI experience the same problem: all profiles show the age of 38. Does anyone have a solution available?
Comment #5
droople commentedbump
Comment #6
rightchoice2c_me commentedWork 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;
Comment #7
kreativalab commentedtry 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
Comment #8
sivaji_ganesh_jojodae commentedI 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.
Comment #9
foodbo commentedyup, 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
Comment #10
mmachina commentedi just can't seem to get it to work... anyone else have similar problems?
Comment #11
eL commentedI 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?
Comment #12
nopu commentedGuys all these code is not working can any one please let me know for drupal7 site how to calculate the age for dob field.
Comment #13
silvi.addweb commentedFor solving this issue add this code into Computed Code (PHP) box field setting.
Let me know if you face any query/concern regarding this.
Thanks!
Comment #14
dqdDue 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.