How do I show age from a birthdate?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Blade
Forum Newbie
Posts: 6
Joined: Mon May 27, 2002 7:14 am

How do I show age from a birthdate?

Post by Blade »

Hi.

I need to show how old a person is from his/her birthdate.
The birthdates come from a mysql db.

I found this script on php.net, but its not working 100%
-----

Code: Select all

<?php
$nowdate = mktime(0,0,0,date("m"),date("d"),date("Y"));
$birthday = mktime(0,0,0,"05","22","1982"); 
$age=intval(($nowdate-$birthday)/(60*60*24*365));
?>
-----

So I would appreciate it, if someone had a solution for me.


Blade
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

you may get this directly by a mysql-query

Code: Select all

$query = "SELECT DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(birthday)), '%Y') AS age FROM user";
$result = mysql_query($query);
Blade
Forum Newbie
Posts: 6
Joined: Mon May 27, 2002 7:14 am

Post by Blade »

Yay, that did it..

Thanks for the fast reply :)

Gotta love those sql statements. My select statement is aprox. 800 chars long now, with 9 joins :)

Oh, just one thing, is it possible to remove those zero'es at the begining of the result? I mean without using php.

/Blade
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

jepp
$query = "SELECT DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(birthday)), '%Y')+0 AS age FROM user";
Blade
Forum Newbie
Posts: 6
Joined: Mon May 27, 2002 7:14 am

Post by Blade »

oh yeah, of course that old hack..

Thanks again :)
Post Reply