Created
October 31, 2012 18:09
-
-
Save kyleweiner/3988770 to your computer and use it in GitHub Desktop.
PHP: A function that returns an array of years (relative to the current year) using an age range.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// return an array of years (relative to the current year) using an age range | |
function getYearsByAgeRange($minAge = 0, $maxAge = 0) | |
{ | |
if ( ! is_numeric($minAge) || ! is_numeric($maxAge) || $minAge > $maxAge) return; | |
$thisYear = date('Y'); | |
$maxYear = $thisYear - $minAge; // e.g. 2012 - 11 = 2001 | |
$minYear = $thisYear - $maxAge; // e.g. 2012 - 105 = 1907 | |
$years = array(); | |
for ($i = $maxYear; $i >= $minYear; $i--) | |
{ | |
array_push($years, $i); | |
} | |
return $years; | |
} | |
$yearsArray = getYearsByAgeRange(11, 105); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment