Calculate age based on date of birth
Moderator: General Moderators
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
You need these lines as well.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.
Code: Select all
if(($birth[1] > date("m")) || ($birth[1] == date("m") && date("d") < $birth[2]))
{
$age -= 1;
}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.
- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
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));
}heh, just had a need to rewrite it..
set it and forget 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;
}Re: Calculate age based on date of birth
take it and use it immediately
Re: Calculate age based on date of birth
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;
}
Re: Calculate age based on date of birth
The code in the first post did have a bug, I'll update it. The code posted a few posts up works fine.
[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]
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);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]
- waytoecommerce
- Forum Newbie
- Posts: 16
- Joined: Tue Apr 12, 2011 11:47 am
Re: Calculate age based on date of birth
Here is also one method that may help you out.
please apply and check :
(round((DATEDIFF(NOW(), 1980-03-13)/365))
please apply and check :
(round((DATEDIFF(NOW(), 1980-03-13)/365))