Jump to content

Calculating Age


matfish

Recommended Posts

Hi,

Im trying to find out how to calculate the correct age from a given date of birth. I did use just the years (this year 2006 - date pf birth year) but this is inaccurate as surely it should also go by day and month?

Anyone help?

Thanks
Link to comment
Share on other sites

At the moment I could only thinks of a troublesome method.

First include this to your script
[code]$day = date('d'); // This will check the current date
$month = date('m'); // This will check the current month
$year = date('Y'); // This will check the current year[/code]

After that you just include a form that let the user choose their date of birth and use those informations above to calculate.

Example:
[code]$_POST['year'] -  $year[/code]
This will give you the year.
Link to comment
Share on other sites

Ummm, That would have been right in 1920. Unix time functions don't work on dates before '1970-01-01'

Try

[code]<?php
function age($dt)  {

$y1 = date('Y', strtotime($dt));
$y2 = date('Y');
$md1 = date('md', strtotime($dt));
$md2 = date('md');
return ($md2 < $md1) ? ($y2 - $y1 - 1) : ($y2 - $y1);
}

$dob = '1949-01-22';

echo age($dob);
?>[/code]
Link to comment
Share on other sites

[quote author=Barand link=topic=101345.msg401042#msg401042 date=1153472554]
Ummm, That would have been right in 1920. Unix time functions don't work on dates before '1970-01-01'
[/quote][quote author=brown2005 link=topic=101345.msg401128#msg401128 date=1153484750]
well that wasnt my own code, it was the code someone else gave to me, so i will change it to your code..

SO NOT MY FAULT
[/quote]

That is a variation of some code I posted the other week, looking back on it, it's not the most ideal solution (there's a bug in there too I have noticed). But it does work on dates prior to the epoc on some installs. Negative timestamps are supported on a *NIX/PHP5 combo I believe...
Link to comment
Share on other sites

[code]
function age($dt)  {
    $t = strtotime($dt);
   
$y1 = date('Y', $t);
$y2 = date('Y');
$md1 = date('md', $t);
$md2 = date('md');
if ($md2 < $md1) {
$yrs = ($y2 - $y1 - 1);
$mths = 12 + date('m') - date('m', $t);
}
else {
    $yrs = ($y2 - $y1);
    $mths = date('m') - date('m', $t);
}
return "$yrs years $mths months";
}
[/code]
Link to comment
Share on other sites

Barands code:

[code]<?php
function age($dt)  {

$y1 = date('Y', strtotime($dt));
$y2 = date('Y');
$md1 = date('md', strtotime($dt));
$md2 = date('md');
return ($md2 < $md1) ? ($y2 - $y1 - 1) : ($y2 - $y1);
}

$dob = '1949-01-22';

echo age($dob);
?>[/code]

works a treat!

Many thanks!!!!
Link to comment
Share on other sites

[quote=SA]That is a variation of some code I posted the other week, looking back on it, it's not the most ideal solution (there's a bug in there too I have noticed). But it does work on dates prior to the epoc on some installs. Negative timestamps are supported on a *NIX/PHP5 combo I believe...[/quote]

Looks as though it works even on windows so long at its year 1901+. Guess the earlier example using 1900 was just unlucky.
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.