Jump to content

[SOLVED] age


ki

Recommended Posts

<?php
$ageTime = mktime(0, 0, 0, 7, 3, 1978); // Get the person's birthday timestamp
$t = time(); // Store current time for consistency
$age = ($ageTime < 0) ? ( $t + ($ageTime * -1) ) : $t - $ageTime;
$year = 60 * 60 * 24 * 365;
$ageYears = $age / $year;

echo 'You are ' . floor($ageYears) . ' years old.';
?>

Link to comment
https://forums.phpfreaks.com/topic/57968-solved-age/#findComment-287329
Share on other sites

try this. Subtract the years but if the actual birth date has not yet been reached this year then subtract 1

 

<?php
function getAge ($dob)
{
    $tob = strtotime($dob);
    $age = date('Y') - date('Y', $tob);
    return (date('md') < date('md', $tob)) ? $age-1 : $age;
}

echo getAge ('1949-01-22').'<br>'; //58
echo getAge ('1945-12-16').'<br>'; //61

?>

Link to comment
https://forums.phpfreaks.com/topic/57968-solved-age/#findComment-287349
Share on other sites

Simelar to Barand's answer.

Thouhg you must input the entire Date Of Birth..

 

DD/MM/YYYY

function Getage($str)
{
$dob = explode("/", $str);
$year = $dob[2];
$month = $dob[1];
$day = $dob[0];

$year_to = date("Y");
$month_to = date("m");
$day_to = date("d");

$age_this_year = $year_to - $year;
$age_minus_one = $age_this_year - 1;

if($month_to == $month)
{
	if ($day_to >= $day)
	{
		$age = $age_this_year;
	}
	else
	{
		$age = $age_minus_one;
	}

}
else
{
	if($month_to > $month)
	{
		$age = $age_this_year;
	}
	else
	{
		$age = $age_minus_one;
	}	
}
return $age;
}

Link to comment
https://forums.phpfreaks.com/topic/57968-solved-age/#findComment-287353
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.