Hi, I need to get someone's age in years + months for different dates. For some reason, my input check isn't working and I keep getting a mktime error? Is there an easier way to return false if dates aren't splitting or numeric?
Thanks for any help!
function getAge($date, $futureDate=false)
{
if(preg_match('/^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/', $date) === false) { return false; }
elseif($futureDate!=false && preg_match('/^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/', $futureDate) === false) { return false; }
$p = split('[/.-]', trim($date));
$start = mktime(0, 0, 0, $p[0], $p[1], $p[2]);
$end = mktime(); // 1970s unix bday
if($futureDate!==false) {
$p = split('[/.-]', trim($futureDate)); // delimiter can be slash, dot, or hyphen
$end = mktime(0, 0, 0, $p[0], $p[1], $p[2]);
}
if(!$start || !$end || !$p) {
return false; }
$age = abs($end - $start);
$y = $age / (60 * 60 * 24 * 365);
$m = strstr(($age / (60 * 60 * 24 * 365)), '.') * 12; // remainder of months
$h = $age / (60 * 60 * 24); // hours in period
return array(
'y' => floor($y),
'm' => floor($m),
'h' => floor($h),
's' => $age
);
}