Calculate age based on date of birth

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

What is the rest of your code? There could be things making that age different.
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

olrac wrote:Hello,

I have a question for this code is this the right bdate code?
because i tried these code and isn't working i mean it does but my age is 22. on that code it show my age is 23.

$dat=explode("-",$rs->fields[bdate]);
$age=(date("Y")- $dat[0]);

Thank you.
You need these lines as well.

Code: Select all

if(($birth[1] > date("m")) || ($birth[1] == date("m") && date("d") < $birth[2]))
	{
		$age -= 1; 
	}
See it would probably subtract one year from the value giving your correct data. You need the whole routine I posted.
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

I am working on a project with a friend of mine who suggested i use this bit of maths...and he doesn't even dabble in php much.

Code: Select all

//removed - didnt work as one would hope from simple things.
Last edited by qads on Mon Jun 18, 2007 3:46 am, edited 2 times in total.
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

Hate to tell you but it won't be acurate for every day. :)
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

heh, thanks for the heads up.

I'll remove the code in the post.
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

Right, this should be good for any month, returns your exact age down to a decimal point.

Code: Select all

function age($month, $year)
{
return (date("Y")+(date("m")/12)) - ($year+($month/12));
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Exact age... relative to the month. It's no more accurate than that. :P
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Just glancing at that, I would say it most likely doesn't take leap years into account, and therefore is not 100% accurate.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

:lol:
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

heh, just had a need to rewrite it..

Code: Select all

 
function get_age($year, $month, $day) {
  if (date('m') - (int)$month >= 0) {
      if (date("d") - (int)$day >= 0 || date('m') > $month) {
          return date('Y') - (int)($year);
      }
  }
  return date('Y') - (int)($year) - 1;
}
set it and forget it..
pkphp
Forum Newbie
Posts: 12
Joined: Mon Sep 20, 2010 1:20 am

Re: Calculate age based on date of birth

Post by pkphp »

take it and use it immediately
MichaelR
Forum Contributor
Posts: 148
Joined: Sat Jan 03, 2009 3:27 pm

Re: Calculate age based on date of birth

Post by MichaelR »

The OP's code fails if the the birth date's day is greater than the current date's day even if the month of the former is less than the latter (which, of course, gives the wrong result). I'd suggest the following modification:

Code: Select all


    function getAge($date) {

      list($year, $month, $day) = explode('-', $date);

      $age = date('Y') - $year;

      if (($month > date('m')) || ($month == date('m') && $day > date('d'))) {
        return $age - 1;
      }

      return $age;

    }
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Calculate age based on date of birth

Post by Benjamin »

The code in the first post did have a bug, I'll update it. The code posted a few posts up works fine.

Code: Select all

function get_age($year, $month, $day) {
  if (date('m') - (int)$month >= 0) {
      if (date("d") - (int)$day >= 0 || date('m') > $month) {
          echo "Age is ", date('Y') - (int)($year), " if born on $month/$day/$year<br />";
          return;
      }
  }
  echo "Age is ", date('Y') - (int)($year) - 1, " if born on $year/$day/$year<br />";
}

get_age(2000, 1, 1);
get_age(2000, 10, 1);
get_age(2000, 1, 30);
get_age(2000, 10, 19);
get_age(2000, 10, 20);
get_age(2000, 10, 21);
get_age(2000, 12, 1);
get_age(2000, 12, 24);
[text]Age is 10 if born on 1/1/2000
Age is 10 if born on 10/1/2000
Age is 10 if born on 1/30/2000
Age is 10 if born on 10/19/2000
Age is 10 if born on 10/20/2000
Age is 9 if born on 2000/21/2000
Age is 9 if born on 2000/1/2000
Age is 9 if born on 2000/24/2000[/text]
User avatar
waytoecommerce
Forum Newbie
Posts: 16
Joined: Tue Apr 12, 2011 11:47 am

Re: Calculate age based on date of birth

Post by waytoecommerce »

Here is also one method that may help you out.

please apply and check :

(round((DATEDIFF(NOW(), 1980-03-13)/365))
Post Reply