Gossamer Forum
Home : Products : DBMan : Customization :

Determing age by birthdate

Quote Reply
Determing age by birthdate
I currently have a field called "UserAge" setup, that a user can enter how old they are.

I decided it would be better if they could enter their birthdate and have the program figure out how old they are, so they don't have to keep modifying it every year.

So when they view their account, I could have something like this display:

<TR><TD ALIGN="Right" VALIGN="TOP" WIDTH="20%"><$font_color>Birthdate:</
FONT></TD>
<TD WIDTH="80%"> <$font>$rec{'UserBday'} ($user_age)</Font></TD></TR>

Is there a mod available for this? or could someone point me in the right direction?

Thanks!
Frank
Quote Reply
Re: Determing age by birthdate In reply to
I thought I had done this before and, lo and behold, I was able to find it in the forum! (Yay! Smile )

You can see it at http://www.gossamer-threads.com/...m12/HTML/000290.html . If you have done any changing of the date format, you'll have to change the code a little bit, but I can help you with that.


------------------
JPD





Quote Reply
Re: Determing age by birthdate In reply to
Another great mod! Thanks a bunch!

I have one question though. Even though I specify the format be in DD-MMM-YYYY, I suspect I will be getting a few that won't pay attention, and enter something like
10-20-1971 (MM-DD-YY). The program accepts it, even though it's in the wrong format, and in this particular case, it says I'm 28. When I change it back to the correct format, it works.

So, I was thinking, it there a regular expression I could place in the user.cfg file under the UserAge field definition, that would only accept DD (numeric only), MMM (alpha only), YYYY (numeric only)?

Thanks,
Frank
Quote Reply
Re: Determing age by birthdate In reply to
It should only accept the correct date format if you set the field type to date.

Check to see if that works for you. If not, I think I can come up with a regular expression for you.


------------------
JPD





Quote Reply
Re: Determing age by birthdate In reply to
Ooops... I didn't even think about the field type. I changed it and it worked fine. Smile

Thanks,
Frank
Quote Reply
Re: Determing age by birthdate In reply to
Hi -

I noticed this thread and wanted to update the forum on a similar mod I devised a few weeks ago (with help from JPD, of course Smile). Turned out my code was buggy and I had to separate it into two subroutines, which I added to html.pl. I'm sure there's a more efficient way to code it, but I'm new to Perl and just glad I got it to work.

It's part of a database of membership profiles for a parents' support group, so the dates are converted to ages differently, depending on age: newborns in days or weeks, older babies in months, older kids in years/part years, etc. It allows for a freestyle text field to have dynamic ages embedded, so if the user enters "I have two boys, 20-Jan-1994 and 06-Jul-1998 old", it will be converted (today) to "I have two boys, 5 years and 15 months old".

Code:
sub html_dates_to_ages {

### Expected parameter is a text string containing birthdates.
### (Will also work for anniversaries, etc.) All patterns matching
### the date format dd-mmm-yyyy (case insensitive) will be converted to a
### current age using the results of sub html_date_to_age, which will also
### (sort of) validate the day of month. Year must be between 1900 and 2099.

my ($string) = $_[0];
my ($age) = "?";
my (@dates);

@dates = ($string =~ /\d{2}-(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-(?:19|20)\d{2}/iog) ;

for $dates (@dates) {

$age = &html_date_to_age(lc($dates));
$string =~ s/$dates/$age/ ;
}

return $string;
}

######################

sub html_date_to_age {

### Expected parameter is a date in the form dd-mmm-yyyy.
### The "dd" has not been validated, other than being digits.

my ($bd_day, $bd_month, $bd_year) = split(/-/, $_[0]);

### At this point, if $bd_day is not between 1 and 31 then we'll
### return the original value $_[0] unconverted as it is not a valid date.
### NOTE: This will not catch impossible dates like 31-Feb-1990.

$bd_day = int($bd_day);
unless ($bd_day && ($bd_day <= 31)) {return $_[0];}

### Continuing...

my ($sec, $min, $hour, $day, $month, $year, $dweek, $dyear, $daylight) = localtime(time());
my ($age);
my ($units) = " day"; ### This will be changed if the child's age != 1 day.

my (%months) = ("jan" => 0, "feb" => 1, "mar" => 2, "apr" => 3, "may" => 4, "jun" => 5,
"jul" => 6, "aug" => 7, "sep" => 8, "oct" => 9, "nov" => 10,"dec" => 11);

### Convert $bd_month to a number between 1 and 12.
$bd_month = $months{$bd_month};

### Convert $bd_year to the same format as $year.
$bd_year = $bd_year - 1900;

### Now we can determine the age in days. The constants used are the
### average num of days in a year or month - close enough for this routine.

$age = int( (365.25 * ($year - $bd_year ))
+ ( 30.44 * ($month - $bd_month))
+ ($day - $bd_day) );

### No birthday party until you're born!
if ($age < 0) { return $_[0]; }

### Older than 24 months (730)?
if ($age > 730) {
$age = sprintf("%.1f",$age/365.25); # convert $age to years, round to nearest tenth.
if ($age >= 5) {$age = int($age)} # 5 or older? No need for tenths.
else { $age =~ s/\.0// } # No need for age to end with ".0"

$units = " years";
}
### Between 8 weeks and 24 months? Convert $age to nearest whole month (30.44 days)
elsif ($age > 56) {
$age = int(($age/30.44)+.5);
$units = " months";
}
### Between 2 weeks and 8 weeks? Convert $age to nearest whole week
elsif ($age >= 14) {
$age = int(($age/7)+.5);
$units = " weeks";
}
### Use correct grammar
elsif ($age != 1) { $units = " days"; }

### Return the converted string in colored text. The color indicates that it is "dynamic",
### so if someone enters an age directly (not dynamic) then viewers will be able to tell
### that it's not necessarily current. If you prefer regular text color, use...
### return ($age . $units);
return ('<FONT COLOR=navy>' . $age . $units . '</FONT>');
}

It will not work with autogenerated record displays. To apply the conversion to a field called "Children", you'd simply find "$rec{'Children'}" in sub html_record, and replace it with &html_dates_to_ages($rec{'Children'})

Another cool use for this would be something like a profile field in which someone could enter "We have been married for 25-Dec-1990 and still love each other!" Wink

Scott
noelles@teleport.com
Quote Reply
Re: Determing age by birthdate In reply to
JPD-

I noticed a small problem with this mod. If a user enters a birthdate that is in the future, they're age shows up as a negative number.

Is there a way to only allow them to enter a birth date before a certain set date? I would assume nobody under the age of 10 would be interested in registering on my site.

Thanks,
Frank
Quote Reply
Re: Determing age by birthdate In reply to
hi,
I have been reading lot of threads about "Age mods" that user enter their brithday (date ) into the record...when user view the record, it will show up the "age" instead of brithday. Also,people can enter the "age" instead of brithday to search on the record (like searching for age 16 - 24 ).
JPDeni have written some nice mods for it...but its kinda confusing which one i should use because there are more than one threads talking about this "Age mod".
Could any one tell me the exactly coding of "Age mod" that i use for the above situation ??

Any suggestion would be greatly apprciated