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!
Someone willing to provide a "how old are you" function.
By given date...to tell how old a person is.
Yes I know...this is a sign of me being lazy , just thought someone would like to spit it out
Thanks.
dethron wrote:try to write it which relies on timestamp
Timestamps will not work for people's ages.
They won't?
If one wanted to use a timestamp, they could use the function strtotime() to get the timestamp of a user entered date quickly. Then the next step is to make a simple math function that'll calculate how many seconds have passed since that timestamp until now. Then take that number of seconds and turn it into years/months/days using some more simple math.
<?php
$birthday = strtotime("6 September 1984");
$now = time();
$elapsed = $now - $birthday;
$days = round ($elapsed / 86400, 1); // 86400 is how many seconds are in a day
echo $days;
?>
It would display 8140.5. Which, if you divide that by 365, gives you my age which is 22 and some change.
dethron wrote:try to write it which relies on timestamp
Timestamps will not work for people's ages.
They won't?
If one wanted to use a timestamp, they could use the function strtotime() to get the timestamp of a user entered date quickly. Then the next step is to make a simple math function that'll calculate how many seconds have passed since that timestamp until now. Then take that number of seconds and turn it into years/months/days using some more simple math.
<?php
$birthday = strtotime("6 September 1984");
$now = time();
$elapsed = $now - $birthday;
$days = round ($elapsed / 86400, 1); // 86400 is how many seconds are in a day
echo $days;
?>
It would display 8140.5. Which, if you divide that by 365, gives you my age which is 22 and some change.
What's the timestamp of someone who's twenty years older than you?