Thursday, December 13, 2007

Calculating age from DOB:php

$age = strtotime(date("Y-m-d")) - strtotime($dob);
$age = round( $age/30758400);


Powered by ScribeFire.

4 comments:

  1. Thanks, this is very very useful.

    ReplyDelete
  2. how did you arrive at your magic number of 30758400 ?

    it looks like you need the number of seconds in a year (avg)

    and it looks like you multiplied seconds(60) x minutes(60) x hours(24) x 356 days ??

    there are usually 365 days in a year (on earth at least) and 365.25 would be better to describe leap years.

    from this i got... 31557600

    ReplyDelete
  3. another solution.. [ from php.net]
    $dob = '1984-09-04';
    $age = date('Y') - date('Y', strtotime($dob));
    if (date('md') < date('md', strtotime($dob))) {
    $age--;
    }

    Subhankar
    http://aafter.com

    ReplyDelete