how old are you function ?

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
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

how old are you function ?

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Thou shall not be lazy... There... i've spit it out :P
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

:)

ok
I found one here

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

looks pretty promising.
thanks :)
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

try to write it which relies on timestamp ;)
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

dethron wrote:try to write it which relies on timestamp ;)
hm...why should I? what benefit will it bring?
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post by iknownothing »

its friendlier to functions that other methods.
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post 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 ;)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

dethron wrote:try to write it which relies on timestamp ;)
Timestamps will not work for people's ages.
printf
Forum Contributor
Posts: 173
Joined: Wed Jan 12, 2005 5:24 pm

Post 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
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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');

?>
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

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