Bluehost.com Web Hosting $6.95

Re: subtracting dates...

This is a discussion on Re: subtracting dates... within the PHP General forums, part of the PHP Programming Forums category; On Sat, Aug 02, 2003 at 08:30:34PM +0100, John Ryan wrote: > > In mySQL, I store dates ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 08-02-2003
Paul Chvostek
 
Posts: n/a
Default Re: subtracting dates...

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 <paul@it.ca>
it.canada http://www.it.ca/
Free PHP web hosting! http://www.it.ca/web/

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 12:04 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0