Re: Calculating a persons age based on their birthday.
by cciulla (Friar) on May 03, 2000 at 03:19 UTC
|
use Date::Calc qw( Decode_Date_EU Today leap_year Delta_Days );
$date = <STDIN>; # get birthday
($year1,$month1,$day1) = Decode_Date_EU($date);
($year2,$month2,$day2) = Today();
if (($day1 == 29) && ($month1 == 2) && !leap_year($year2))
{ $day1--; }
if ( (($year2 - $year1) > 18) ||
( (($year2 - $year1) == 18) &&
(Delta_Days($year2,$month1,$day1, $year2,$month2,$day2) >= 0) ) )
{
print "Ok - you are over 18.\n";
}
else
{
print "Sorry - you aren't 18 yet!\n";
}
| [reply] [d/l] |
Re: Calculating a persons age based on their birthday.
by buzzcutbuddha (Chaplain) on May 03, 2000 at 15:50 UTC
|
IMHO a better way to tell if someone's age in years is to calc this way:
use integer; # speeds up the math
my @date = localtime(time);
# convert year to months and add current number of months
my $yearnow = ((($date[5] + 1900) * 12) + ($date[4] + 1));
print "Enter an 8 digit birthdate. eg: 01/01/1970\n";
my ($monththen, $daythen, $yearthen) = split /\//, <STDIN>;
# we'll fly without error-checking now and just add what they e
+ntered for month
my $modyearthen = (($yearthen * 12) + ($monththen));
# subtract the totals and divide by 12, convert to int (redunda
+nt for safety sake)
# and voila actual number of years alive
$numyears = int (($yearnow - $modyearthen)/12);
print "You've been around $numyears years!\n";
This way, you are also accounting for the months. For example, I was born in
August, and if you just subtract the years from each other right now, it says I'm 25,
which isn't right. You need to account for the total number of months, and then take
the integer part of the division by 12 which gives you the year and remainder. It's more
work, but it's a hair more accurate. Certainly for the sake of accuracy, you could take
it down to days...but I'll leave that to someone else! :) | [reply] [d/l] |
RE: Calculating a persons age based on their birthday.
by Simplicus (Monk) on May 03, 2000 at 20:20 UTC
|
If all you want is the "year value" of a person's age, you could divide by 365, after you added one to the number of days for each of the leap years since the person's birth. You could calculate this offset by subtracting their birth year from the current year and dividing the result by four. This works becuase they can't have been born both before and after Feb 29th. Something like:
use integer;
#...code ommited; obtain $days from FROM_DAYS()
# and $birth_year from database
my $this_year;
my @time;
my $raw_years;
my $num_leaps;
my $int_years;
my $float_years;
@time = localtime(time);
$this_year = $time[5] + 1900;
$raw_years = $this_year - $birth_year;
$num_leaps = $raw_years / 4;
$days += $num_leaps;
$int_years = $days / 365;
no integer;
$float_years = $days / 365;
if ($int_years == $float_years) {
print "Today's your birthday!";
}
#...Then do whatever else you're going to do
I used a few more variable declarations than I would normally but I wanted to clarify what I was doing. It's quick and dirty, but probably works...
Simplicus | [reply] [d/l] |
|
| [reply] |
|
| [reply] |
Re: Calculating a persons age based on their birthday.
by btrott (Parson) on May 03, 2000 at 03:13 UTC
|
| [reply] |
|
I was just looking for the number of years alive.
So like when their birthday rolls around I can say happy
birthday your 10 years old. Or hey everyone John Doe is
10 years old today.
| [reply] |
|
Date::Calc (or, heaven forbid, Date::Manip) is probably
overkill since you only want age in years. All you really
want to know is whether the birthday has passed this year.
sub age {
# Assuming $birth_month is 0..11
my ($birth_day, $birth_month, $birth_year) = @_;
my ($day, $month, $year) = (localtime)[3..5];
$year += 1900;
my $age = $year - $birth_year;
$age-- unless sprintf("%02d%02d", $month, $day)
>= sprintf("%02d%02d", $birth_month, $birth_day);
return $age;
}
Of course, if you're only using this on a person's birthday,
all you need to do is subtract their birth year from the
current year :-) | [reply] [d/l] |
|
|
|
|
|
Re: Calculating a persons age based on their birthday.
by Maqs (Deacon) on May 03, 2000 at 14:12 UTC
|
As far as I understand, you would like to know the number of full years the person is alive?
So, assuming you have only the number of day, we can do the following:
---
my $year = (localtime)[5]+1900;
my $fullyears=0;
$nod = xxxx; #number of days goes here...
while ($nod > 365)
{ if ( $year % 4) {$nod=$nod-365} else {$nod=$nod-366}; #check for the leap year.
$year--; $fullyears++;
};
print "$fullyears\n";
---
Rather simple but m.b. not so gracefull solution :) You see, you do not need any special modules.
| [reply] |
|
if ($year % 4) {$nod=$nod-365} else {$nod=$nod-366}; #check for the le
+ap year.
be
if ((($year % 4 == 0) && ($year % 100 != 0)) || ($year % 400) == 0) {$
+nod=$nod-365} else {$nod=$nod-366}; #check for the leap year.
To handle Y2K (and others) correct...
/t0mas | [reply] [d/l] [select] |
|
if ((($year % 4 == 0) && ($year % 100 != 0)) || ($year % 400) == 0) {$
+nod=$nod-366} else {$nod=$nod-365}; #check for the leap year.
365 and 366 swiched places....
| [reply] [d/l] |
|