Solved: PHP help, trying to calculate my age

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

namenotfound

Thread Starter
Joined
Apr 30, 2005
Messages
3,012
I'm trying to figure out how to calculate my age in PHP so on my site I don't have to update my profile page each year.

So far I figured out how to display how old I'll be within the given year

PHP:
<?php
$thisyear=date("Y");
echo ($thisyear-1981);
?>
But I also need to factor in the month and date. I've been playing around with it for a few minutes, trying different things.

Does anyone know how to factor in the month and date when calculating my age?
 
Joined
Jul 8, 2002
Messages
14,681
Get the timestamp for when you were born (use gmmktime() to do this). Then do time() - $timestamp_at_birth and you'll have the number of seconds since you were born. Divide by 31536000 and you've got your age in years.
 

namenotfound

Thread Starter
Joined
Apr 30, 2005
Messages
3,012
Ok, I'm getting the number 24.178049625824
I'm guessing everything to the right of the dot is how much time I have left before I'm 25?

How do I remove that part and JUST have it display "24"?

This is what I put
PHP:
<?php
$timestamp_at_birth=gmmktime(0, 0, 0, 11, 19, 1981);
//(hour, minute, second, month, day, year)
$myage = time() - $timestamp_at_birth;
echo ($myage/31536000);
?>
 

jiml8

Guest
Joined
Jul 2, 2005
Messages
2,634
brendandonhu said:
Get the timestamp for when you were born (use gmmktime() to do this). Then do time() - $timestamp_at_birth and you'll have the number of seconds since you were born. Divide by 31536000 and you've got your age in years.
What about leap year?
 
Joined
Jul 8, 2002
Messages
14,681
Depending on which time libraries his server uses, it may or may not be affected by a leap year.
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top