Using CCK, Date Field, and Computed field to determine age, and astrological sign
I created a date field called field_bday using CCK in my profile node type, then created a computed field type called age.
Here are the settings I used for the age field.
Label - Age
Widget Type - Computed
Help text - Blank
Default Value PHP Code - Blank
Required - not checked off
Number of values - 1
Computed Code:
if (!$node->nid) node_save($node);
$dob = $node->field_bday[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;
field_bday in the second line of code is the variable name for the birthdate field I make using the date widget.
Display this field - checked off.
Display Format -
$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
Now... I just need to figure out how to do a computed field to determine the western astrological sign, and possibly the Chinese one as well. I may make little images for each one just to make it cute but that can wait.
Any thoughts on what PHP would go in Computed Code in order to determine the astrological sign? Once I have the western one I can use it as an example to make the Chinese one as well. Smiling I will also post them attached to this question as well so people can use it/them.
Computed Field - http://drupal.org/project/computed_field
Date Field - http://drupal.org/project/date
CCK - http://drupal.org/project/cck

Download
Download http://drupal.org/project/birthdays and look at the .module file. In there you will find code which computes astrological signs. :)
--
CiviHosting - Drupal Hosting at its Best
I checked it out, its not
I checked it out, its not compatible with content profile.
i just want to calculate age
Passionate_Lass, i tried your code but its not working.
Are you sure it works on your end?
Keep in mind i'm just looking for a way to calculate age, not the horoscope stuff.
Yes my code works fine on my
Yes my code works fine on my end. Using my birthdate it gives me the correct age.
Computed Code:
if (!$node->nid) node_save($node);
$dob = $node->field_bday[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;
Display Format:
$display = $node_field_item['value'];Remember that you need to change field_bday to whatever the fieldname is in your case.
Computed age field
Passionate_Lass, that worked now, thank you!
Yay. I coded the astrological
Yay.
I coded the astrological sign, and the Chinese horoscope field as well. If you want the code for those let me know.
it works! what a great
it works! what a great thing!!!!!
thank you so much! thank you so much!
why can't i get this one working?
no matter which code example i tried, it all ends up in
"The default value PHP code returned an incorrect value."
btw: is it important how the birthday-date is stored?
- Jo
Check this detailed tutorial
Check this detailed tutorial I wrote. I believe that yes the way it is stored is probably important.
http://drupalsn.com/learn-drupal/drupal-tutorials/user-age-date-birth-%E...
astrological sign
Passionate_Lass,
many thanks for your age computed field it is very useful for my project.
Can You post the code for astrological field too, or send it to on my e-mail?
bigbroz(at)mail.ru
Check this tutorial that I
Check this tutorial that I wrote. I added a couple comments with the snippets.
http://drupalsn.com/learn-drupal/drupal-tutorials/user-age-date-birth-%E...
Аstrological sign
I am also interested in
ymakux(here is 'at')gmail.com
Thanks!
Check this tutorial that I
Check this tutorial that I wrote. I added a couple comments with the snippets.
http://drupalsn.com/learn-drupal/drupal-tutorials/user-age-date-birth-%E...
Age not updated
Using CCK+date+computed field:
I just discovered that the age is not updated after somebody had a birthday, something I would really expect because "Store using the database settings below" is NOT checked in the settings of this computed field...
I just use Views Bulk
I just use Views Bulk Operations to update them.
Anonymous Users?
Is there a way to provide this feature in a block to anonymous users? I want any user to be able to select their birth date and then have their sign displayed.
Thanks in advance!
I get the age 39 if the user
I get the age 39 if the user does not fill out the "date of birth" field.
How can i handle that? I like to get a n.a. or something when the user will not fill this out.
Is the birthday field requiered in you case?
I get the age 39
I get the age 39 if the user does not fill out the "date of birth" field.
How can i handle that? I like to get a n.a. or something when the user will not fill this out.
Is the birthday field requiered in you case?