Re: [PHP] Age from date field?

2003-10-12 Thread Eugene Lee
On Sun, Oct 12, 2003 at 02:09:38AM +0200, DvDmanDT wrote:
: 
: Does anyone have a good solution on how to get the age of someone from a
: date column in mysql... This is what I have, but it's not really the
: truth... What's the right way to do it?
: 
: floor((time()-$a[born])/(3600*24*365.25))
: 
: where $a[born] is the timestamp of the birthdate... Current query:

This should work for well-formed timestamps, i.e. they are not in the
future:

function age($ts)
{
list($y1, $m1, $d1) = explode(' ', date('Y m d', $ts));
list($y2, $m2, $d2) = explode(' ', date('Y m d', time()));
$age = $y2 - $y1 - ((($m2  $m1) || ($d2  $d1)) ? 1 : 0);
return $age;
}

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Age from date field?

2003-10-12 Thread Curt Zirzow
* Thus wrote Eugene Lee ([EMAIL PROTECTED]):
 On Sun, Oct 12, 2003 at 02:09:38AM +0200, DvDmanDT wrote:
 : 
 : Does anyone have a good solution on how to get the age of someone from a
 : date column in mysql... This is what I have, but it's not really the
 : truth... What's the right way to do it?
 : 
 : floor((time()-$a[born])/(3600*24*365.25))
 : 
 : where $a[born] is the timestamp of the birthdate... Current query:
 
 This should work for well-formed timestamps, i.e. they are not in the
 future:
 
 function age($ts)
 {
 list($y1, $m1, $d1) = explode(' ', date('Y m d', $ts));
 list($y2, $m2, $d2) = explode(' ', date('Y m d', time()));
 $age = $y2 - $y1 - ((($m2  $m1) || ($d2  $d1)) ? 1 : 0);

What happens if the month is the same but the day hasnt been
reached?


Shouldnt this be:
 $age = $y2 - $y1 - ((($m2  $m1) || ($m2 == $m1  $d2  $d1)) ? 1 : 0);

To account for it being the same month but not yet the day.


Curt
-- 
My PHP key is worn out

  PHP List stats since 1997: 
  http://zirzow.dyndns.org/html/mlists/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Age from date field?

2003-10-12 Thread Eugene Lee
On Sun, Oct 12, 2003 at 02:49:53PM +, Curt Zirzow wrote:
: * Thus wrote Eugene Lee ([EMAIL PROTECTED]):
:  On Sun, Oct 12, 2003 at 02:09:38AM +0200, DvDmanDT wrote:
:  : 
:  : Does anyone have a good solution on how to get the age of someone from a
:  : date column in mysql... This is what I have, but it's not really the
:  : truth... What's the right way to do it?
:  : 
:  : floor((time()-$a[born])/(3600*24*365.25))
:  : 
:  : where $a[born] is the timestamp of the birthdate... Current query:
:  
:  This should work for well-formed timestamps, i.e. they are not in the
:  future:
:  
:  function age($ts)
:  {
:  list($y1, $m1, $d1) = explode(' ', date('Y m d', $ts));
:  list($y2, $m2, $d2) = explode(' ', date('Y m d', time()));
:  $age = $y2 - $y1 - ((($m2  $m1) || ($d2  $d1)) ? 1 : 0);
: 
: What happens if the month is the same but the day hasnt been
: reached?
: 
: Shouldnt this be:
:  $age = $y2 - $y1 - ((($m2  $m1) || ($m2 == $m1  $d2  $d1)) ? 1 : 0);
: 
: To account for it being the same month but not yet the day.

You're right.  I got lost trying to short-circuit the expression and
forgot to explicitly test for same month.  :-)

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Age from date field?

2003-10-11 Thread DvDmanDT
Does anyone have a good solution on how to get the age of someone from a
date column in mysql... This is what I have, but it's not really the
truth... What's the right way to do it?

floor((time()-$a[born])/(3600*24*365.25))

where $a[born] is the timestamp of the birthdate... Current query:

SELECT UNIX_TIMESTAMP(birthdate) as born FROM members WHERE id='1'

So... how would I do it... Doesn't matter if it's MySQL or PHP that
calculates it, as long as I can expect it to work on paid hosts...

Any suggestions? Notice that I'm only looking for the age in years... Thanks
in advance
-- 
// DvDmanDT
MSN: [EMAIL PROTECTED]
Mail: [EMAIL PROTECTED]



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php