Using CCK, Date Field, and Computed field to determine age, and astrological sign

Passionate_Lass - April 1, 2009 - 18:44

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

HershelSR - April 1, 2009 - 18:57

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

Passionate_Lass - April 3, 2009 - 00:28

I checked it out, its not compatible with content profile.

i just want to calculate age

doomed - April 2, 2009 - 03:43

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

Passionate_Lass - April 3, 2009 - 00:32

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

doomed - April 3, 2009 - 21:27

Passionate_Lass, that worked now, thank you!

Yay. I coded the astrological

Passionate_Lass - April 4, 2009 - 00:12

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

Buffer2k - April 12, 2009 - 23:23

it works! what a great thing!!!!!

thank you so much! thank you so much!

why can't i get this one working?

der_sensemann - April 16, 2009 - 10:39

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

Passionate_Lass - April 26, 2009 - 04:32

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

BigBroz - April 17, 2009 - 10:33

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

Passionate_Lass - April 26, 2009 - 04:33

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

volocuga - April 22, 2009 - 20:26

I am also interested in

ymakux(here is 'at')gmail.com

Thanks!

Check this tutorial that I

Passionate_Lass - April 26, 2009 - 04:34

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

askibinski - August 19, 2009 - 08:22

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

Buffer2k - September 23, 2009 - 13:03

I just use Views Bulk Operations to update them.

Anonymous Users?

mgladding - September 19, 2009 - 23:40

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

autoberater - November 11, 2009 - 12:41

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

autoberater - November 11, 2009 - 12:42

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?

 
 

Drupal is a registered trademark of Dries Buytaert.