Jump to content

date to age


Kingy

Recommended Posts

in my user database the users have there date of birth stored as DD MM YYYY

eg: (23 Mar 1986).

 

What i was wondering is can i turn that into their age. eg the above example would come out as 21

 

would i need to explode the DD MM YYYY or could i just leave it as is?

Link to comment
Share on other sites

No you would not need to do this, almost all these actions can be found in the date and time functions in PHP.

 

<?php

$date = "23 Jan 1998";

$thisyear = date("Y");

$birthyear = date("Y", strtotime($date));

$age = $thisyear - $birthyear;

echo $age;
?>

 

Please note I have not done it completely for you.

Link to comment
Share on other sites

<?php
$dob = '23 jan 1998';

echo calcAge ($dob);             

function calcAge ($dob)
{
    $tob = strtotime($dob);
    $age = date('Y') - date('Y', $tob);    
    return date('md') < date('md', $tob) ? $age-1 : $age;
}

?>

 

I recommend you store dates in a DATE type column (format yyyy-mm-dd). Your format looks pretty but is useless as a db date format.

Link to comment
Share on other sites

okay yer right, i think it's the leap years.

 

No. Example:

Person is born at June 1st, 2000. The current year is 2008, so if you say 2008-2000 then you'll have calculated the persons age to be 8, but it has not yet been the person's 8th birthday this year, so the actual age is 7. Barand's calcAge() takes that into account.

Link to comment
Share on other sites

Any method that just takes a difference between numbers (it does not matter if the numbers are in seconds or the day number of the year) will be off by one day for every leap year that has occurred since their DOB. This will mean that near the birthday, the calculation will be incorrect.

 

The code that Barand posted is the definition of a person's age. It even works if your birthday is on February 29th. If a DOB is stored as a DATETIME in a database, you can do the same calculation in a SELECT query (there is an example in the "pets" tutorial section in the mysql manual.)

Link to comment
Share on other sites

That's why Juluan Calander functions are better than the norm, they take account for leap years in the equation.

Julian was before the gregorian calander (what we use today), but julian calander was a lot more accurate.

 

from way back in my C days, there used to be a julian date toolbox, that had all these nifty date functions.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.