Jump to content

Calculating age


cordoprod

Recommended Posts

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
https://forums.phpfreaks.com/topic/103385-calculating-age/#findComment-529443
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
https://forums.phpfreaks.com/topic/103385-calculating-age/#findComment-529448
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
https://forums.phpfreaks.com/topic/103385-calculating-age/#findComment-529449
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
https://forums.phpfreaks.com/topic/103385-calculating-age/#findComment-529475
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.