[SOLVED] Show age

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
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

[SOLVED] Show age

Post by Dale »

I have 3 fields in my database

DOBDAY
DOBMONTH
DOBYEAR

So when a person registers it adds it as:
17
November
1987

But i wanted to know, how i can get them 3 fields to be converted and shows the age. (eg; 17)
Last edited by Dale on Thu Apr 28, 2005 5:54 pm, edited 1 time in total.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Because your month is stored as a sting use the strtotime function in PHP.

And then do a date comparison.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

I just checked MYsql has a function also.

STR_TO_DATE(str,format)

http://dev.mysql.com/doc/mysql/en/date- ... tions.html
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

hawleyjr wrote:Because your month is stored as a sting use the strtotime function in PHP.

And then do a date comparison.

I'll use this PHP way. However now that (eg; my birthday) shows a whole number:

Code: Select all

<?php
$dob = strtotime("7 November 1987");
echo $dob;
?>
Now that shows: 563259600
Now how do i change that value to an age?
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

that's 7 November 1987 in unix timestamp format.

You'll need the current time - that time and convert it from seconds to years.

Code: Select all

$age = (time() - strtotime("7 November 1987")) * 60 * 60 * 24 * 365;
I think that's right...
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

That gives me:

Code: Select all

1.7390945588112E+16
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

hahaha. change all the * to /
XD goofy me.
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

Skara wrote:hahaha. change all the * to /
XD goofy me.

Cool, that works, now how do i make the result as a whole number?
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

floor($foo);
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

Skara wrote:floor($foo);
Thank you so! much! *kiss*
Post Reply