It’s as follows :-
sub age {
use Time::Local;
use Date::Calc qw(Delta_Days);
# Assuming $birth_month is 0..11
my ($birth_day, $birth_month, $birth_year) = @_;
my ($day, $month, $year) = (localtime)[3,4,5];
$year += 1900;
$month+=1;
my @today_sec = localtime();
my $time = timelocal(@today_sec);
my @birthday_sec = (0, 0, 0, $birth_day, $birth_month, $birth_year);
my $birthtime = timelocal(@birthday_sec);
my @birthday=($birth_year, $birth_month, $birth_day);
my @today=($year, $month, $day);
my $days = Delta_Days(@birthday, @today);
my $netage="";
if($days > 28) {
my $age = $year - $birth_year;
$age-- unless sprintf("%02d%02d", $month, $day)
>= sprintf("%02d%02d", $birth_month, $birth_day);
my $mnth=($month>$birth_month)?$month-$birth_month:12+($month-$birth_month);
my $mnth_total=($age*12)+$mnth;
if($mnth_total > 0 && $mnth_total < 4) {
my $week=int($days/7);
if($week > 1) {
$netage = $week." Weeks";
} else {
$netage = $week." Week";
}
} elsif($mnth_total >= 4 && $mnth_total < 24) {
$netage = $mnth_total." Months";
} elsif($age >= 2 && $age < 18) {
$netage = $age." Years";
if($mnth == 1) {
$netage .= " ".$mnth." Month";
} elsif($mnth > 1) {
$netage .= " ".$mnth." Months";
} else { }
} elsif($age >= 18) {
$netage = $age." Years";
} else { }
} else {
$netage = $days." Days";
}
return $netage;
}
print &age(6,4,1990);
Output :-
24 Years