Return to Snippet

Revision: 32498
at September 27, 2010 07:30 by phifty


Initial Code
function calculateAge ($birthdate)
{
	$bdt = strtotime($birthdate);
	
	if (false === $bdt || -1 === $bdt) {
		return 0;
	}
	
	$birthDateParts = getdate($bdt);	
	$todayDateParts = getdate();
	
	$nYear = $todayDateParts['year'] - $birthDateParts['year'];
	$nMonth = $todayDateParts['mon'] - $birthDateParts['mon'];
	if ($nMonth < 1) {
		if ($nMonth == 0) {
			$nDay = $todayDateParts['mday'] - $birthDateParts['mday'];
			return ($nDay < 0) ? ($nYear - 1) : $nYear;
		}
		
		return ($nYear - 1);
	}
	
	return $nYear;
}

Initial URL

                                

Initial Description

                                

Initial Title
Another way to calculate age, given a MySQL date format

Initial Tags
date

Initial Language
PHP