Hello, I'd really appreciate if someone can help me with this issue.
I have a profile for users, in this profile I have field "Date of birth" in which the users can enter the date (it is field of type Date).
I also have a computed field called Age, in which I calculate the value using the information entered in the date of birth field.

Finally I have a view in which a user can search persons, so, in that view I need a new field in which the users can search by range, something like this:

Age ranged: (then in a dropdown list)
x- 15-20
x- 20-30
x- 30-35
etc..

I would like to know if there is an easy (I really don't care if it's easy... :)) way to add this functionality to the view.

Obviously thanks a lot for all the tips that you can give me.

Claudio.
-** www.fileando.com **-

Comments

lias’s picture

I've also created a date of birth date field although I haven't been able to figure out the compute age field. Would you mind sharing what you entered in your computed field to come up with the "age" of the user?

Thank you very much Claudio! I will also research the dropdown selection or another means of searching by age group or maybe just by age?
Lsabug

cacciaresi’s picture

Of course I can share how to calculate the age my friend :)

Well first of all you need to add a Computed Field (http://drupal.org/project/computed_field).

In the Data settings Panel, add this code (you should change the value of "field_fecha_de_nacimiento" to your corresponding birth date field):

if (!$node->nid) node_save($node);

$today = time();
$date_of_birth = date_make_date($node->field_fecha_de_nacimiento[0]['value']);
$date_parts = $date_of_birth->db->parts;

$year_diff = date("Y") - ($date_parts['year']);
$month_diff = date("m") - ($date_parts['mon']);
$day_diff = date("d") - ($date_parts['day']);

    if ($month_diff < 0)
      $year_diff--;
    elseif ($month_diff == 0 && $day_diff < 0)
      $year_diff--;

$node_field[0]['value'] = $year_diff;

Then, check "Display this field".
In display format add this: $display = $node_field_item['value'];
also check this: "Store using the database settings below".

Well, I think that with this things it should work ok!

I finally give up, searching for a solution to this, but I really think if we dedicate some time it should´t be so hard to make a search by ranges.
Claudio Acciaresi
-* www.fileando.com *-

Railslide’s picture

It doesn't work for me! It just displays 2008 instead fo the user age... Any idea of what could be wrong?

Railslide’s picture

IIt works for me with this code (that I've taken from macgirvin http://drupal.org/node/211000 and I've modified a bit)

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; 

you have just to change dob with your field's name

patchak’s picture

Thank you, this code worked perfectly!

site web lausanne
Actualité informatique

ximo’s picture

I'd like to know this as well, perhaps it could be possible through theming?

lias’s picture

Thank you for sharing, there was also this php snippet from Michelle for calculating age, it goes in a template file depending on your content type and you don't need compute field. Switch out the field with your birthday field.

<?php
        $birthdate = $node->field_birthdate[0]['value'];
        $now = time();
        $then = strtotime($birthdate);
        $diff = date('Y', $now) - date('Y', $then);
        print $diff;
        ?>
hansrossel’s picture

I think the snippet of cacciaresi is better and more complete for calculating age as it also takes months and days into account. The snippet of Michelle only takes the difference between years which is not correct, certainly in cases where it is important to know if somebody is over 18 or not.

Hans
KOBA - Webdesign with Drupal

Anonymous’s picture

I'm currently implementing the same thing with years and search range. Maybe it's worth looking at this patch: http://drupal.org/node/151989