- array_shift: documentation ( source)
- explode: documentation ( source)
- stripos: documentation ( source)
<?php
/**
* ageCheck method
*
* This is a custom validation method that converts the date of birth into an age and then checks to see if the age falls within the teams level age range
*
* @param array $check This is the value for date of birth field
* @param string $level_id This is the level_id selected for this competitor's team
* @return bool
*/
function ageCheck($check, $level_id) {
$dob = array_shift($check);
$dob = new DateTime($dob);
$today = new DateTime('today');
$age = $dob->diff($today)->y;
$age_range = 'n/a-n/a';
$age_range_arr = explode('-', $age_range);
if (stripos($age_range,'N/A') !== false) { return true; }
if ($age >= $age_range_arr[0] && $age <= $age_range_arr[1]) {
return true;
}
return 'You must select a date of birth where the competitor\'s age is between' . $age_range_arr[0] . '-' . $age_range_arr[1];
}
echo ageCheck(array('dob' => '1978-11-21'), NULL);