Here's a function I wrote to turn a range of ages into a pair of date-of-birth boundaries for use in database queries (or wherever else a date-of-birth boundary is more useful than an age range). Comments and suggestions for improvement are welcome.
Updated: prettier variable names, better error handling.
Updated: Removing use of now()
=item DoBrangefromAges REFERENCEDATE MINAGE MAXAGE Given a REFERENCEDATE from which to calculate, minimum age MINAGE, and an optional maximum age MAXAGE, this function returns two strings in YYYY-MM-DD format, suitable for use in SQL queries, e.g., 'WHERE ?<dob AND dob<?', using the return values in order as parameters. If no MAXAGE is given, date range is for the year spanning MINAGE only. =cut sub DoBrangefromAges { my ($querydate,$agemin,$agemax,$inclusive) = @_; die "[E] Minimum age omitted in DoBrangefromAges" unless (defined +$agemin and $agemin ne ''); $agemin = int($agemin); $agemax = int($agemin) unless defined $agemax; $agemax = int($agemax); $inclusive = ($inclusive ? $inclusive : 0); my ($maxdob,$mindob) = ($querydate,$querydate); $maxdob->subtract(years => $agemin); $mindob->subtract(years => $agemax + 1); return $mindob->ymd('-'),$maxdob->ymd('-'); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Get DoB Bounds from Age Range
by jeffa (Bishop) on May 07, 2015 at 16:30 UTC | |
by over2sd (Beadle) on May 07, 2015 at 17:22 UTC | |
by wrog (Friar) on May 07, 2015 at 17:57 UTC | |
by over2sd (Beadle) on May 08, 2015 at 01:39 UTC | |
by jeffa (Bishop) on May 07, 2015 at 17:38 UTC | |
by over2sd (Beadle) on May 07, 2015 at 21:40 UTC | |
|
Re: Get DoB Bounds from Age Range
by wrog (Friar) on May 10, 2015 at 17:43 UTC | |
by over2sd (Beadle) on May 11, 2015 at 19:18 UTC | |
|
Re: Get DoB Bounds from Age Range
by Laurent_R (Canon) on May 16, 2015 at 15:58 UTC |