|
|
|
|
Thread title: Age Calculation Script |
|
|
|
|
|
Thread tools
Search this thread
Display Modes
|
|
08-12-2008, 07:45 PM
|
#1
|
Status: Jakowenko.com
Join date: Jun 2005
Location: Michigan
Expertise:
Software:
Posts: 2,199
|
Age Calculation Script
I have a script I use to calculate the age of users in my database
PHP Code:
<?
function calculate_age ($month, $day, $year) {
$ageTime = mktime(0, 0, 0, $month, $day, $year); // Get the person's birthday timestamp
$t = time(); // Store current time for consistency
$age = ($ageTime < 0) ? ( $t + ($ageTime * -1) ) : $t - $ageTime;
$year = 60 * 60 * 24 * 365;
$ageYears = $age / $year;
$actual=floor($ageYears);
return $actual;
}
?>
When I input something like 12-6-1989 it returns an age of 18 which is correct.
But when I was inputing an age of 12-16-1899 it returns an age of 38. Any idea why?
|
|
08-12-2008, 07:55 PM
|
#2
|
Status: Junior Member
Join date: Jul 2008
Location: Abilene TX
Expertise: Design, PHP, JS, HTML5, CSS3
Software:
Posts: 37
|
mktime converts your date into the number of seconds since Jan 1, 1970, so when putting in a date before that, your math has to be different. Make sense?
|
|
08-12-2008, 08:14 PM
|
#3
|
Status: Geek
Join date: Apr 2006
Location: Denver, CO
Expertise: Software
Software: Chrome, Notepad++
Posts: 6,894
|
for two reasons:
1. A date before 1/1/1970 is a negitave number, which makes the math different.
2. Your date is beyond the limits of an integer on a 32 bit system (-2,147,483,647 opposed to -2,239,056,000). PHP converts the variable to a float which makes things really complicated and inconsistent. Either way, it wont work.
|
|
08-12-2008, 10:03 PM
|
#4
|
Status: Request a custom title
Join date: Feb 2006
Location: Nottingham
Expertise:
Software:
Posts: 1,648
|
As aforesaid. Nonetheless, my first contribution in such a long while will exalt you, mi amigo:
PHP Code:
/* Oh, Wildhoney to the rescue! TalkPHP.com loves you. */
function date_age($iBirthDay, $iBirthMonth, $iBirthYear)
{
/* Determine the current date, sunshine. */
list($iCurrentDay, $iCurrentMonth, $iCurrentYear) = array(date('j'), date('n'), date('Y'));
/* Quick guess -- maybe incorrect guess at the age. */
$iAge = $iCurrentYear - $iBirthYear;
/* Was the individual born before the current month, if so, we were right first time. */
if ($iBirthMonth < $iCurrentMonth)
{
return $iAge;
}
/* It's my birthday and I'll cry if I want to. */
if ($iBirthDay <= $iCurrentDay && $iBirthMonth == $iCurrentMonth)
{
/* Without a time, we'll assume the individual's birth time has passed. */
return $iAge;
}
/* Lucky individual still has birthday presents to look forward to sometime THIS year. */
return $iAge - 1;
}
printf("Age: %d<br />", date_age(10, 10, 1985));
printf("Age: %d<br />", date_age(10, 2, 1985));
printf("Age: %d<br />", date_age(12, 8, 1985));
printf("Age: %d<br />", date_age(12, 6, 1989));
printf("Age: %d<br />", date_age(12, 16, 1899));
|
|
08-14-2008, 09:40 PM
|
#5
|
Status: Request a custom title
Join date: Apr 2007
Location: UK
Expertise: Code & Programming
Software: Coda, TextMate, Sublime 2
Posts: 2,097
|
|
|
|
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
|