Jump to content

Calculating age


cordoprod

Recommended Posts


<?php
$ageTime = mktime(0, 0, 0, 9, 9, 1919); 
$t = time(); 
$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
Share on other sites

I'm sure the above works fine, but I couldn't understand half of it. >_>

 

anyway, this is the way I would have done it.

 

<?php

 

$birthTime = mktime(0,0,0,11,27,1987);

$birthYear = strftime("%Y", $birthTime); //get's the birth year

$birthMonth = strftime("%m", $birthTime); //get's birth month

 

$currentYear = strftime( "%Y", time() ); //get's the current year

$currentMonth = strftime( "%m", time() ); //get's current month

 

$age = $currentYear - $birthYear; //your age

 

if ($birthMonth > $currentMonth)

$age--; //if your birth month hasn't past, you aren't a year older!

 

echo "You are ". $age . "year's old!";

?>

Link to comment
Share on other sites

This code will work, and will account for leap years as well. There may be a cleaner way of writing it, but it works. You will need to fill in the code for $old_timestamp, as it is the timestamp you referred to in your post.

 

<?php
$old_timestamp = _____; // This is the timestamp you referred to in your post. You need to fill in the code.
$old_year = date("Y", $old_timestamp);
$old_month = date("n", $old_timestamp);
$new_timetamp = time();
$new_year = date("Y", $new_timetamp);
$new_month = date("n", $new_timetamp);
if($old_month != $new_month)
{
$age = ($old_month < $new_month) ? $new_year - $old_year : $new_year - $old_year - 1;
}
else
{
$old_day = date("j", $old_timestamp);
$new_day = date("j", $new_timetamp);
$age = ($old_day <= $new_day) ? $new_year - $old_year : $new_year - $old_year - 1;
}

echo ($age != 0) ? "age: {$age}" : "age: 0";
?>

Link to comment
Share on other sites

oh my, lack of sleep is not good >_> you totally got me.

 

for my code, you'd have to add in another line that gets the day using "%D";

 

and then an else if ((birthmonth == currentmonth) && (birthday < currentday))

 

>_> i think by now, I'd just use "zenag's" code, tho I really wish he added comments.

Link to comment
Share on other sites

There is no need to do the month/day calculation separately. Here is a shorter version with some test data (uses yyyy-mm-dd as the DOB format, but can be changed to anything) -

 

<?php
// calculate age using php
// (Current year - birth year) - 1 (if current mm-dd < birthday mm-dd)
$dates = array();
$dates[] = "2008-01-16";
$dates[] = "2007-01-15";
$dates[] = "2007-01-16";
$dates[] = "2007-01-17";
$dates[] = "1900-01-15";
$dates[] = "1958-11-11";
$dates[] = "2006-01-16";

function year($date)
{
return substr($date, 0, 4); 
} // end of year function

function mmdd($date)
{
return substr($date, 5,5);
} // end of mmdd function

function age($today,$date)
{

return year($today) - year($date) - (mmdd($today) < mmdd($date));
} // end of function age

$today = date("Y-m-d");
foreach($dates as $date)
{
echo "DOB: $date, Age: " . age($today,$date) . "<br />";
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.