...

# Date of birth function

karlosio
12-29-2006, 06:06 PM
Here is a snippet for quickly calculating and displaying how old a person is, which will update itself each year

<?php
function dateDiff(\$dformat, \$endDate, \$beginDate)
{
\$date_parts1=explode(\$dformat, \$beginDate);
\$date_parts2=explode(\$dformat, \$endDate);
\$start_date=gregoriantojd(\$date_parts1[0], \$date_parts1[1], \$date_parts1[2]);
\$end_date=gregoriantojd(\$date_parts2[0], \$date_parts2[1], \$date_parts2[2]);
return \$end_date - \$start_date;
}
//Enter date of birth below in MM/DD/YYYY
\$dob="11/12/1980";
echo round(dateDiff("/", date("m/d/Y", time()), \$dob)/365, 0) . " years.";
?>

Example output:

26 Years

xconspirisist
12-29-2006, 06:29 PM
That's rather a long and bloated way of doing things. Why not do something like this?

/**
* Takes a standard string describing a date, and calculates how many years
* have passed between now and then. In this purpose, used to calculate how
* old somebody is.
*
* @author xconspirisist <xconspirisist@gmail.com>
*/
function yearsOld(\$dob) {
\$dob = strtotime(\$dob);

if (\$dob === false) {
echo 'Your supplied date could not be parsed by strtotime()';
return;
}

\$elapsedSeconds = mktime() - \$dob;

// There are 31556926 seconds in a year.
\$yearsOld = \$elapsedSeconds / 31556926;
\$yearsOld = floor(\$yearsOld);

return \$yearsOld;

}

echo 'You are ' . yearsOld('20th August 1987') . ' years old.';

guelphdad
01-05-2007, 07:47 PM
There are 31556926 seconds in a year
are you basing this on 365.27 days? are you accounting for leap years? or are you just going for ballpark figures?

felgall
01-05-2007, 10:46 PM
365.27 is wrong (and has no relationship to the number of seconds specified earlier) there are 365.242803 days in a year on average which is 31556977 seconds. A few seconds probably doesn't make any significant difference to the calculation given that you are working on an average year length and so it needs to round up or down to a whole day.

ralph l mayo
01-08-2007, 10:11 PM
You can let strtotime (correctly) account for leap years if it worries you:

function years_old(\$dob)
{
if ((\$dob = strtotime(\$dob)) === false)
{
throw new Exception('strtotime parsing fails it');
}
for (\$i = 0; strtotime("-\$i year") > \$dob; ++\$i);
return \$i - 1;
}

You're far more likely to encounter problems from timezone discrepancies and from people not entering the hour and minute of their birth than you are from losing seconds over the years, though. At least for users who aren't several thousand years old.

guelphdad
01-09-2007, 05:44 AM
365.27 is wrong (and has no relationship to the number of seconds specified earlier) there are 365.242803 days in a year on average which is 31556977 seconds. A few seconds probably doesn't make any significant difference to the calculation given that you are working on an average year length and so it needs to round up or down to a whole day. thanks, yes I realize that my number was wrong, I didn't look it up and had remembered it incorrectly. I didn't know if the fraction of days had been taken into account, that is all.

xconspirisist
01-16-2007, 04:56 AM
I would of hoped from my description, that I was posting rather basic code, but still - those are interesting points. Thanks for your advice.

modern bedroom
05-21-2007, 08:46 PM
It's an interesting little program but i personally argue whit time. I was thinking giving up clocks but this is almost impossible. I don't like to measure the time passing, i want to ignore it as much as i can.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum