Age not correct
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?
Site contents are licensed under a
try this...
<?phpif (!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
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
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?