how should i get the age from the date of birth
| Project: | Computed Field |
| Version: | 5.x-1.1 |
| Component: | Code |
| Category: | support request |
| Priority: | critical |
| Assigned: | solutionpoint |
| Status: | active |
Jump to:
Hello all ,
i tried a lot but unable to find and fix the problem.
i want to calculate the age of a user from his/her DOB field but how to do i dont no.
1. i created a computed field and passed the code to it also in the following way :
$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 = int
Data length = 2
Default value = 0
Required = yes
but doesn't worked for me .......only the default value is showing = 0
| Attachment | Size |
|---|---|
| Member Profile_1227694778457.png | 17.52 KB |
| Now Age-1227694875167.png | 10.1 KB |
| Now Age_1227694846208.png | 13.28 KB |

#1
the computed fields i created for age calculation is Now Age.
Please some body help out for solving this problem this is very urgent
Thanks and Regards
Kunal
#2
Take a look at this: http://drupal.org/node/149237
#3
i have seen this and made changes also but i am not getting the result so please let me explain clearly step by step
for which i shall remain ever thankful to you ..
thanks
kunal
#4
Greetings,
I was having a similar problem, but was able to fix it.
I should mention that I am using the 6.x-1.0-beta2 version.
Here is what I am using for computed code:
if (!$node->nid) node_save($node);
$dob = $node->field_dob[0]['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[0]['value'] = $diff;
Here is what is in the display field:
$display = $node_field_item['value'];Database:
Store using the database settings below: checked
Data type: Int
Data length: 3
Default Value: **left blank**
Not NULL: checked
Sortable: checked
Also make sure that in the above code where I'm using "field_dob" that you replace this with "field_(the name of your field that is providing the birthdate)" In this case I am usind "dob" because "field_dob" is the name of my Date field that holds the birthdate I'm using to calculate age.
When I initially created this I received a "0" as the computed age. I tried re-saving it by opening the node for edit (but not editing anything) and then hitting "save" while in edit mode and that got the correct computed age figure to display. So, for me the key was re-saving the node.
I'm fairly new to Drupal and still learning my way around so I can't say exactly why this worked at this point but maybe someone else can lend some insight into why the problem occurred initially and why the above fix worked.
Thanks,
Scott