You would think that determining a person’s age would be a very simple task for a PHP developer. However, when I asked this question for myself I found a variety of solutions that people posted online. Many of them used large functions with conditionals and other logic that was hard to follow. After a significant amount of research, I’ve come to find the shortest and most accurate solution to this question.
function current_age($birthdate) {
return( floor((time()-strtotime($birthdate))/31536000) );
}
You can pass just about any type date that strtotime() can recognize, which is an added convenience. If you have a better solution, let’s hear about it in the comments.
This function doesn’t check for leap years and can’t calculate the correct age in every case.
This is true, but it’s very accurate nevertheless. I’m still searching for a 100% perfect solution for calculating age, so please feel free to post if you come up with anything better.
Just a blindshot, didn’t look much at this function, but that should be it. http://de3.php.net/manual/en/datetime.diff.php
else, well, I use the same function as you, as it is accurate enough for me.
Better solution?
http://my.fit.edu/~aaudu/age.html