#### Archived

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

# calculating age

## Recommended Posts

I want to calculate the age of some one.
so I want to substract 2 dates and then keep the year.
at this point I get the years of today's date and substract the year of the birthday date of the person.
but that means that every body's age changes at new year.

\$now = date("Y-m-d");
\$bday = \$Row["UserBirthday"];
\$age = \$now-\$bday;

is how I calculate the age of someone

I guess there is a way to substract two dates and round down to the year.

so does anybody kno how I can get the right number of years difference ?

anatak

##### Share on other sites
My suggestion would be to use strtotime() to reduce the 2 dates to the unix timestamp and do your calculations based off of that.

##### Share on other sites
Your code may not return anything meaningful:
[code]<?php
\$now = date("Y-m-d");
\$bday = \$Row["UserBirthday"];
\$age = \$now-\$bday;
?>[/code]
The value of "\$now" is a string, the subtraction might work because PHP will automatically turn a string like "2006-03-23" into 2006.

What you should do is something like:
[code]<?php
\$hour = 3600; // number of seconds in an hour
\$day = hour * 24; //number of seconds in a day (86400)
\$yr = \$day * 365.25; // number of seconds in a year, approximately
\$now = strtotime('today'); // the number of seconds since 1-1-1970
\$bday = strtotime(\$Row['UserBirthday']);  // I'm assuming that the format of \$Row['UserBirthday'] is yyyy-mm-dd
\$age = floor(\$now - \$bday)/\$yr;
echo \$age;
?>[/code]

This algorithm will work all the time if the birthday is on or after 1-1-1970. On some systems it might work for earlier dates.

Ken

##### Share on other sites
here is my solution.
because of the problem wiht the range of dates I wrote the following function

here is the code that calls the function age()

[code]
If (substr(\$Row["UserBirthday"], 0, 4)=="0000"){
\$date=display_date_format(\$Row['UserBirthday']);
\$bday=substr(\$date, 0,strlen(\$date)-4).'secret';
\$age='secret';
}
else{

\$today = date("Y-m-d");
\$bday = \$Row["UserBirthday"];
\$age= age(\$today, \$bday);
echo "function age: ".\$age;
//    \$age = \$today-\$bday;
\$bday = display_date_format(\$Row["UserBirthday"]);
}
[/code]

and here is the function age()
[code]
function age(\$arg01, \$arg02)
{
\$today=\$arg01;
\$bday=\$arg02;
echo \$today ." today<br>";
echo \$bday . " birthday<br>";
\$todayyear=substr(\$today, 0, 4);
\$todaymonth=substr(\$today, 5, 2);
\$todayday=substr(\$today, 8, 2);

\$bdayyear=substr(\$bday, 0, 4);
\$bdaymonth=substr(\$bday, 5, 2);
\$bdayday=substr(\$bday, 8, 2);

\$age = \$today - \$bdayyear;
if(\$todaymonth-\$bdaymonth<=0){
echo \$age." month age -1<br>";
\$age--;
}elseif(\$todayday-\$bdayday<0){
echo \$age." day age -1<br>";
\$age--;
}

return \$age;
}
[/code]
this works for me
thank you for looking into my problem
anatak

×

• Chat

## 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.