Age not correct

Age not correct

  • Visitor
  • 604
  • 1969-12-31
  • Offline
  • Tue, 01/01/2008 - 07:42

Hiho,
the field "age" isn't computed correctly. Look at my profile. My birthdate is 28.11.1983 and that means I am 24 years old, but the age field says "25".

How can I change:
$diff = date('Y', $now) - date('Y', $then);

so it works correctly?


try this...

  • Tue, 01/08/2008 - 07:36

<?php
       
if (!empty($node->content['field_birthdate']['#value'])) {
        print
"<tr><td><span class=\"field-item\"><strong>" . t('Age') . ": </strong></span>";
           
$birthdate = $node->field_birthdate[0]['value'];
           
$then = strtotime($birthdate);   
       
$dob_year = date('Y', $then);
       
$dob_month = date('n', $then);
       
$dob_day = date('d', $then);
       
$diff format_date(time(), 'custom', 'Y') - $dob_year - (format_date(time(), 'custom', 'nd') < $dob_month . str_pad($day, 2, 0, STR_PAD_LEFT));
            print
$diff;
            print
"</td></tr>";
    }
   
?>

this is the complete snippet from my profile, maybe u need to adjust the name of the profile field or the css class. this snippet is also tweaked to only show up when a user has entered his/her birthday.

for your understanding: the former calculation just takes the year into account, so in your case it just does 2008-1983, which indeed equals 25. this snippet also takes the month and day into account, if necessary.

i got a little inspired by the birthday module


Thanks it works now

  • Matze (not verified)
  • 604
  • 1969-12-31
  • Offline
  • Sat, 01/12/2008 - 07:39

Thanks it works now correct!

my code looks like this:

        <?php

        
if (!empty($node->field_birthdate[0]['value'])) {
           
$birthdate = $node->field_birthdate[0]['value'];
           
$then = strtotime($birthdate);
           
$dob_year = date('Y', $then);
           
$dob_month = date('n', $then);
           
$dob_day = date('d', $then);
           
$diff = format_date(time(), 'custom', 'Y') - $dob_year - (format_date(time(), 'custom', 'nd') < $dob_month . str_pad($day, 2, 0, STR_PAD_LEFT));
            print
"<strong>Alter: </strong>" . $diff;

        } else {
            print
"<strong>Alter: </strong>keine Angabe";
}
       
       
?>


Need help

  • nf1 (not verified)
  • 604
  • 1969-12-31
  • Offline
  • Sun, 07/13/2008 - 10:47

I'm a newbie to drupal and having problem to display the actual years on the profile. I tried copying the snippets that are posted here but with no luck. I have field in my node profile with name birthdate and pasted the code on the panels page and on the mini manels but nothing happens. What am I doing wrong?


Post new comment
The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd><blockquote>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options