Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,471
There are 1197 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

Age Calculation Script

Thread title: Age Calculation Script
Closed Thread    
    Thread tools Search this thread Display Modes  
08-12-2008, 07:45 PM
#1
Jako is offline Jako
Jako's Avatar
Status: Jakowenko.com
Join date: Jun 2005
Location: Michigan
Expertise:
Software:
 
Posts: 2,199
iTrader: 3 / 100%
 

Jako is on a distinguished road

  Old  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(000$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
infinivert is offline infinivert
infinivert's Avatar
Status: Junior Member
Join date: Jul 2008
Location: Abilene TX
Expertise: Design, PHP, JS, HTML5, CSS3
Software:
 
Posts: 37
iTrader: 0 / 0%
 

infinivert is on a distinguished road

Send a message via AIM to infinivert

  Old

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
Village Genius is offline Village Genius
Village Genius's Avatar
Status: Geek
Join date: Apr 2006
Location: Denver, CO
Expertise: Software
Software: Chrome, Notepad++
 
Posts: 6,894
iTrader: 18 / 100%
 

Village Genius will become famous soon enough

  Old

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
Wildhoney is offline Wildhoney
Wildhoney's Avatar
Status: Request a custom title
Join date: Feb 2006
Location: Nottingham
Expertise:
Software:
 
Posts: 1,648
iTrader: 18 / 95%
 

Wildhoney is on a distinguished road

Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney

  Old

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(10101985));
printf("Age: %d<br />"date_age(1021985));
printf("Age: %d<br />"date_age(1281985));
printf("Age: %d<br />"date_age(1261989));
printf("Age: %d<br />"date_age(12161899)); 

08-14-2008, 09:40 PM
#5
Gaz is offline Gaz
Gaz's Avatar
Status: Request a custom title
Join date: Apr 2007
Location: UK
Expertise: Code & Programming
Software: Coda, TextMate, Sublime 2
 
Posts: 2,097
iTrader: 26 / 100%
 

Gaz will become famous soon enough Gaz will become famous soon enough

Send a message via Skype™ to Gaz

  Old

Wildhoney is amazing.

Closed Thread    


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed