TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 08-12-2008, 08:44 PM   #1 (permalink)
The Wanderer
 
Join Date: Jun 2005
Posts: 20
Thanks: 1
Jako is on a distinguished road
Default 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?
Jako is offline  
Reply With Quote
Old 08-13-2008, 02:47 PM   #2 (permalink)
The Contributor
 
buggabill's Avatar
 
Join Date: Jan 2008
Location: Maine, USA
Posts: 92
Thanks: 2
buggabill is on a distinguished road
Default

The Unix epoch which mktime uses starts on January 1, 1970. Wikipedia Article

Someone born in 1970 would be 38 today.

You will need to some modifications to that script to get accurate ages before that date.

You could do something like the following:
php Code:
<?php
function calculate_age ($month, $day, $year) {
    $year_diff = date("Y") - ($year);
    $month_diff = date("m") - ($month);
    $day_diff = date("d") - ($day);
   
    if ($month_diff < 0) {
        $year_diff--;
    }
    elseif ($month_diff == 0 && $day_diff < 0) {
        $year_diff--;
    }

    return $year_diff;
}
?>
__________________
-- Bill
"Why is it drug addicts and computer aficionados are both called users?" -Clifford Stoll
buggabill is offline  
Reply With Quote
Old 08-13-2008, 07:40 PM   #3 (permalink)
The Gregarious
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Cana'derr
Posts: 653
Thanks: 24
delayedinsanity is on a distinguished road
Default

You can still use mktime to get accurate ages.

PHP Code:
function getAge ($month$day$year)
{

    
$now mktime000date("n"), date("j"), date("Y") );
    
$then mktime000$month$day$year );

    
$age floor((((($now $then) / 60) / 60) / 24) / 365.25);

    return 
$age;


delayedinsanity is offline  
Reply With Quote
Old 08-14-2008, 01:47 PM   #4 (permalink)
The Contributor
 
buggabill's Avatar
 
Join Date: Jan 2008
Location: Maine, USA
Posts: 92
Thanks: 2
buggabill is on a distinguished road
Default

Quote:
Originally Posted by delayedinsanity View Post
You can still use mktime to get accurate ages.

PHP Code:
function getAge ($month$day$year)
{

    
$now mktime000date("n"), date("j"), date("Y") );
    
$then mktime000$month$day$year );

    
$age floor((((($now $then) / 60) / 60) / 24) / 365.25);

    return 
$age;


I stand partially corrected.

Take a look at the notes section for mktime.

Quote:
Caution

Before PHP 5.1.0, negative timestamps were not supported under any known version of Windows and some other systems as well. Therefore the range of valid years was limited to 1970 through 2038.
You need to be really careful if what you're writing has any chance of being used on systems like they describe.
__________________
-- Bill
"Why is it drug addicts and computer aficionados are both called users?" -Clifford Stoll
buggabill is offline  
Reply With Quote
Reply



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

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 10:29 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0