Page 1 of 1

how old are you function ?

Posted: Wed Dec 20, 2006 7:14 am
by jmut
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.

Posted: Wed Dec 20, 2006 7:23 am
by timvw
Thou shall not be lazy... There... i've spit it out :P

Posted: Wed Dec 20, 2006 7:31 am
by jmut
:)

ok
I found one here

http://www.zend.com/codex.php?id=614&single=1

looks pretty promising.
thanks :)

Posted: Wed Dec 20, 2006 7:42 am
by dethron
try to write it which relies on timestamp ;)

Posted: Wed Dec 20, 2006 8:16 am
by jmut
dethron wrote:try to write it which relies on timestamp ;)
hm...why should I? what benefit will it bring?

Posted: Wed Dec 20, 2006 8:23 am
by iknownothing
its friendlier to functions that other methods.

Posted: Wed Dec 20, 2006 8:31 am
by dethron
jmut wrote:hm...why should I? what benefit will it bring?
1) instead of passing three different parameters, you can use a date picker and pass one parameter.
2) you dont have to use last if ;)

Posted: Wed Dec 20, 2006 8:38 am
by onion2k
dethron wrote:try to write it which relies on timestamp ;)
Timestamps will not work for people's ages.

Posted: Wed Dec 20, 2006 9:26 am
by printf
Something like...

Code: Select all

<?

function get_age ( $m, $d, $y )
{
	$bd = date ( 'U', strtotime ( $m . '/'. $d . '/' . $y ) );
	$yo = date ( 'Y', time () ) - date ( 'Y', $bd );

	if ( date ( 'z', time () ) < date ( 'z', $bd ) )
	{
		$yo -= 1;
	}

	return ( $yo );
}

$month = 10;
$day = 28;
$year = 1985;

echo get_age ( $month, $day, $year );
		
?>

pif

Posted: Wed Dec 20, 2006 11:35 am
by m3mn0n
onion2k wrote:
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.

example for my birthday:

Code: Select all

<?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.

Posted: Wed Dec 20, 2006 12:08 pm
by bokehman
m3mn0n wrote:They won't?
Maybe a timestamp is ok for whippersnappers but for more mature people a different method is needed.

Code: Select all

<?php

function age($dob /* format Y/m/d */, $delimiter ='/')
{
	if(!function_exists('is_leap'))
	{
		function is_leap($year)
		{
			return (($year%4)?false:(($year%400)?true:false));
		}
	}
	list($year, $month, $day) = explode($delimiter, $dob);
	$days = gregoriantojd($m = date('m'), date('d'), $Y = date('Y'))
	      - gregoriantojd($month, $day, $year);
	$age = 0;
	while($days >= ($remove = (is_leap((($m < 3))?--$Y:$Y--) ? 366 : 365)))
	{
		$days -= $remove;
		$age++;
	}
	return $age;
}

// test it
echo age('1947/12/17');

?>

Posted: Wed Dec 20, 2006 12:51 pm
by onion2k
m3mn0n wrote:
onion2k wrote:
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.

example for my birthday:

Code: Select all

<?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?