Calculate Age from dirth of birth field using date2.0

droople - September 2, 2008 - 15:29
Project:Computed Field
Version:6.x-1.0-beta2
Component:Documentation
Category:support request
Priority:normal
Assigned:Unassigned
Status:active
Description

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

#1

Justin Freeman - September 3, 2008 - 06:58

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?

#2

Casper_BE - September 3, 2008 - 08:25

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

#3

droople - September 3, 2008 - 11:52

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

#4

truelove-nl - October 13, 2008 - 14:53

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

#5

droople - November 19, 2008 - 05:52

bump

#6

rightchoice2c_me - November 19, 2008 - 07:00

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;

#7

lucaclic - December 19, 2008 - 15:57

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

#8

sivaji - May 23, 2009 - 23:50
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.

#9

foodbo - August 14, 2009 - 03:50

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

 
 

Drupal is a registered trademark of Dries Buytaert.