0

When a user registers on my site, their birth date is stored in a mysql database. I'm using the DATE datatype for birth date.

How do I calculate the users current age?

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Madhur Malik
  • 31
  • 1
  • 1
  • 2
  • 1
    What have you tried so far? What problems are you having? Are you able to get the date from mysql? Are you able to convert it to a PHP date? Do you know how to calculate the age from the birthdate? – octern Feb 23 '13 at 18:52
  • http://stackoverflow.com/questions/3776682/php-calculate-age – Deadlock Feb 23 '13 at 18:54
  • @AarolamaBluenk I see what you did there, but those links to Google search results have a tendency to point back to the original post before long. – Wesley Murch Feb 23 '13 at 18:55
  • @WesleyMurch It's just a matter of taking a few moments to see if the problem exists on the inter web. – Kermit Feb 23 '13 at 18:56
  • 1
    @AarolamaBluenk This very post is already the 14th result. – Wesley Murch Feb 23 '13 at 18:57

2 Answers2

6

If you're looking for a PHP solution this works:

$today = new DateTime();
$birthdate = new DateTime("1973-04-18");
$interval = $today->diff($birthdate);
echo $interval->format('%y years');
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • @octern i dont know the whole sequence what to do first then then what after, So please anybody guide me through the whole procedure. How to put everything in a variable so that i echo that variable and i get my answer – Madhur Malik Feb 24 '13 at 18:52
4

You can use one of many date functions in both MySQL and PHP. MySQL example:

SELECT TIMESTAMPDIFF(YEAR,'1980-02-04',NOW()) AS age

Further reading:

dev-null-dweller
  • 29,274
  • 3
  • 65
  • 85