code is used to get age from date. I hope this might help someone like me.
//date in mm/dd/yyyy format; or it can be in other formats as well
$birthDate = "08/14/1972";
//explode the date to get month, day and year
$birthDate = explode("/", $birthDate);
//get age from date or birthdate
$age = (date("md", date("U", mktime(0, 0, 0, $birthDate[1], $birthDate[0], $birthDate[2]))) > date("md") ? ((date("Y")-$birthDate[2])):(date("Y")-$birthDate[2] - 1));
echo "Age is:".$age;
?>
5 comments:
This script is incorrect. It thinks that 15/12/1990 is 20 not 19 years old.
Hi,
Thanks for showing my mistake, it's changed and is working now. The parameters passed to mktime were wrong.
Thanks again
Hi, I tested this and think it is incorrect. Using today's date as the birthdate along with the year 1980, it should return 31, but it in fact returns 30.
Hi,
Thanks for pointing out my mistake. I've resolved this problem. Should work as expected now. Try it once.
Thanks
Still not working properly, used DOB of 07/14/1981 and returns 29 not 30.
Can you explain what is the problem with script?
Thank you!
Post a Comment