On Sat, Aug 02, 2003 at 08:30:34PM +0100, John Ryan wrote:
>
> In mySQL, I store dates as YYYY-MM-DD, a standard DATE type. It stores users
> date of births. I need to calculate in a PHP script, the users age from this
> DOB. I get a PHP date in the same format as the mySQL and subtract, which
> returns the year rounded off. ie, it doesnt matter if your birthdays in june
> of 1983 and the date is januray 2003, your age is still returned as 20, when
> it should be 19.

The function you're probably looking for is floor().

> Does anyone know how can i get the right age?

One quick way would be simple subtraction:

  $old = "1973-06-02";
  $new = "2003-08-02";
  $year = 60*60*24*365.25; // seconds in a year
  $age = strtotime($new) - strtotime($old);
  print "Age in years: " . floor($age / $year) . "\n";

But you'll run into infrequent problems surrounding leap years.  I'm
sure there's a more accurate way to do this, perhaps even using MySQL's
somewhat more advanced (and less timestamp-centric) date calculation
functions, but this is what I can come up with on short notice.

Note that strtotime will give NEGATIVE TIMESTAMPS for dates earlier than
Jan 1st 1970 at midnight GMT, so this still works for folks over 33.  ;)

-- 
  Paul Chvostek                                             <[EMAIL PROTECTED]>
  it.canada                                            http://www.it.ca/
  Free PHP web hosting!                            http://www.it.ca/web/


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

Reply via email to