mysqlperl

Calculate date of birth from age at specific date [MySQL or Perl]


Apologies if this is a really simple question but I am interested in trying to reach an accurate answer and not just a "rounded" up answer.

My problem is: I know somebody is 27.12 on the 18th of March 2008 (random example). How can I calculate, to the nearest approximation, his date of birth. Age is always provided as a real number to two decimal points.


Solution

  • eumiro's answer does the trick; the following, using the Time::Piece module (bundled with Perl since 5.10) is perhaps more maintainable.

    use strict;
    use warnings;
    use 5.010;
    
    use Time::Piece;
    use Time::Seconds;
    
    my ($date, $age) = ('2008-03-18', 27.12);
    
    my $birthday = Time::Piece->strptime($date, '%Y-%m-%d') - $age*ONE_YEAR;
    say $birthday->ymd();
    

    This will get you within a few days of the actual birthday, due to the lack of accuracy (1/100 year) in the age.