# calculating age

3 replies to this topic

### #1 anatak

anatak
• Members
• 407 posts
• LocationJapan, Fukuoka prefecture, Kitakyushu City

Posted 23 March 2006 - 09:10 AM

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
takasi.8008@docomo.ne.jp
tourokum@0508.jp

### #2 ober

ober
• Staff Alumni
• 5,337 posts
• LocationEast Coast, USA

Posted 23 March 2006 - 02:45 PM

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

Info: PHP Manual

### #3 kenrbnsn

kenrbnsn
• Staff Alumni
• 8,235 posts
• LocationHillsborough, NJ, USA

Posted 23 March 2006 - 02:52 PM

Your code may not return anything meaningful:
```<?php
\$now = date("Y-m-d");
\$bday = \$Row["UserBirthday"];
\$age = \$now-\$bday;
?>```
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:
```<?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;
?>```

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

### #4 anatak

anatak
• Members
• 407 posts
• LocationJapan, Fukuoka prefecture, Kitakyushu City

Posted 30 March 2006 - 09:51 AM

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()

```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"]);
}
```

and here is the function age()
```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;
}
```
this works for me
thank you for looking into my problem
anatak

takasi.8008@docomo.ne.jp
tourokum@0508.jp

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users