Jump to content


Photo

Calculating Age


  • Please log in to reply
9 replies to this topic

#1 matfish

matfish
  • Members
  • PipPipPip
  • Advanced Member
  • 242 posts
  • LocationUK

Posted 21 July 2006 - 07:49 AM

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

#2 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 21 July 2006 - 08:31 AM

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

First include this to your script
$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

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

Example:
$_POST['year'] -  $year
This will give you the year.

Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#3 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 21 July 2006 - 08:47 AM

<?php

function age($dob)
{

$date = strtotime($dob);

$today = strtotime(date("d/m/Y"));

echo floor(($today - $date) / 31556926);

}

age("30/03/1900"); //Displays 20

?>

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 21 July 2006 - 09:02 AM

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

Try

<?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);
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 21 July 2006 - 12:25 PM

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

#6 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 21 July 2006 - 12:28 PM

how would you add months on to it...

say like 18 years 4 months....

any ideas?

#7 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 21 July 2006 - 12:55 PM

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

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


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...

#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 21 July 2006 - 01:54 PM

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";
}

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#9 matfish

matfish
  • Members
  • PipPipPip
  • Advanced Member
  • 242 posts
  • LocationUK

Posted 21 July 2006 - 03:47 PM

Barands 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);
?>

works a treat!

Many thanks!!!!

#10 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 21 July 2006 - 05:29 PM

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...


Looks as though it works even on windows so long at its year 1901+. Guess the earlier example using 1900 was just unlucky.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users