Skip to content

Instantly share code, notes, and snippets.

@kyleweiner
Created October 31, 2012 18:09
Show Gist options
  • Save kyleweiner/3988770 to your computer and use it in GitHub Desktop.
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.
// 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