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
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 09-21-2007, 03:11 PM   #1 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Wink Calculating an Age from Date of Birth

If you're particularly nosey then calculating an individual's age from their date of birth can be fascinating. Although there is a way to calculate the age using MySQL alone, MySQL shouldn't really be used as a calculator.

The following function will take a time stamp and output you the user's age.

PHP Code:
function getAge($iTimestamp
{
    
$iAge date('Y') - date('Y'$iTimestamp);
    
    if(
date('n') < date('n'$iTimestamp)) 
    {
        return --
$iAge;
    } 
    elseif(
date('n') == date('n'$iTimestamp)) 
    {
        if(
date('j') < date('j'$iTimestamp)) 
        {
            return 
$iAge 1;
        } 
        else 
        {
            return 
$iAge;
        }
    } 
    else 
    {
        return 
$iAge;
    }

Just remember to feed in a time stamp.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
maZtah (01-10-2009)
Old 09-21-2007, 07:14 PM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

you could just do this
PHP Code:
function bday($day)
{
$age time() - $bday;
$years date("Y",$age);
return 
$years;

Village Idiot is offline  
Reply With Quote
Old 09-21-2007, 07:27 PM   #3 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

I tried that, Village Idiot, but all I appear to get back is 1991:

PHP Code:
echo bday('10-10-1985');

function 
bday($bday)
{
    
$age time() - strtotime($bday);
    
$years date("Y",$age);
    return 
$years;

__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 09-21-2007, 09:58 PM   #4 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

I'm not sure of the logic on Village Idiot's function (1. Get difference between now and birth date in seconds; 2. Push that into date function, which accepts timestamps, not random seconds; 3. Expect to retrieve a year).

Wildhoney's code, whilst I don't see any problems with the functionality, is a wee bit of a mess (mostly because I never really liked elseifs). The code can be squished into the following function which behaves identically to Wildhoney's but, I think, is a little neater.
PHP Code:
function getAgeSalathe($iTimestamp)
{
    
// See http://php.net/date for what the first arguments mean.
    
$iDiffYear  date('Y') - date('Y'$iTimestamp);
    
$iDiffMonth date('n') - date('n'$iTimestamp);
    
$iDiffDay   date('j') - date('j'$iTimestamp);
    
    
// If birthday has not happen yet for this year, subtract 1.
    
if ($iDiffMonth || ($iDiffMonth == && $iDiffDay 0))
    {
        
$iDiffYear--;
    }
        
    return 
$iDiffYear;

Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
maZtah (01-10-2009)
Old 01-04-2011, 10:34 AM   #5 (permalink)
The Visitor
 
Join Date: Jan 2011
Location: india
Posts: 4
Thanks: 0
mojo19 is on a distinguished road
Default brief way for age calculation

<?php
echo "Age Calculator";

date_default_timezone_set('Asia/Calcutta');
$localtime = getdate();

$dob = "21-04-1969";
//$today = date("d-m-Y");
$today = $localtime['mday']."-".$localtime['mon']."-".$localtime['year'];

$dob_a = explode("-", $dob);
$today_a = explode("-", $today);

$dob_d = $dob_a[0];
$dob_m = $dob_a[1];
$dob_y = $dob_a[2];

$today_d = $today_a[0];
$today_m = $today_a[1];
$today_y = $today_a[2];

$years = $today_y - $dob_y;
$months = $today_m - $dob_m;


if ($today_m.$today_d < $dob_m.$dob_d) {
$years--;
$months = 12 + $today_m - $dob_m;

}

if ($today_d < $dob_d) {
$months--;
}

$firstMonths=array(1,3,5,7,8,10,12);
$secondMonths=array(4,6,9,11);
$thirdMonths=array(2);

if($today_m - $dob_m == 1) {
if(in_array($dob_m, $firstMonths)) {
array_push($firstMonths, 0);
}elseif(in_array($dob_m, $secondMonths)) {
array_push($secondMonths, 0);
}elseif(in_array($dob_m, $thirdMonths)) {
array_push($thirdMonths, 0);
}
}
echo "<br><br> Age is $years years $months months.";
?>
mojo19 is offline  
Reply With Quote
Old 01-04-2011, 04:43 PM   #6 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

mojo19, is there any reason why you code completely ignores PHP's built-in tools for working with the date/time? You haven't said whether this is better, or worse, or just different to Wildhoney's old code. Could you perhaps elaborate on why you've posted it, how it is more useful to you (assuming of course, that it is)?
Salathe is offline  
Reply With Quote
Old 01-07-2011, 05:49 AM   #7 (permalink)
The Visitor
 
Join Date: Jan 2011
Location: india
Posts: 4
Thanks: 0
mojo19 is on a distinguished road
Default Hi

Salathe,I didn't use the php built in functions just because they are throwing some warnings while working with them like "date() function may not be reliable because it retrieves the data from the local machine".
mojo19 is offline  
Reply With Quote
Old 01-08-2011, 12:39 AM   #8 (permalink)
The Acquainted
 
wGEric's Avatar
 
Join Date: Nov 2007
Posts: 166
Thanks: 0
wGEric is on a distinguished road
Default

Quote:
Originally Posted by Village Idiot View Post
you could just do this
PHP Code:
function bday($day)
{
$age time() - $day;
$years floor($age 31556926); // 31556926 seconds in a year
return $years;

Fixed. You can do some more math using the remainders to get the months, days, hours, minutes, and seconds.
__________________
Eric
wGEric is offline  
Reply With Quote
Old 01-08-2011, 07:38 AM   #9 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Wow that code I wrote was crap, I dont even remember writing that. But thats common for something I wrote well over three years ago.

Quote:
Originally Posted by mojo19 View Post
Salathe,I didn't use the php built in functions just because they are throwing some warnings while working with them like "date() function may not be reliable because it retrieves the data from the local machine".
That means you don't know how to work with them, it is not PHP's fault. I promise you the built in functions are better than anything you've constructed.

None of it really justifies bumping a >3 year old thread.
__________________

Village Idiot is offline  
Reply With Quote
Old 01-08-2011, 11:51 AM   #10 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Just to bring things a little more up to date, with PHP 5.3.0 came some new classes and functions for working with dates and times. Of use to us is the DateInterval class, and the function (or method) date_diff() (or DateTime::diff()) which returns a DateInterval representing the difference between two DateTime objects.

For example:

PHP Code:
$dob '1066-12-03';
$age date_diff(date_create($dob), date_create('now'))->y
Salathe is offline  
Reply With Quote
Old 01-08-2011, 11:54 AM   #11 (permalink)
The Acquainted
 
Join Date: Nov 2009
Location: nr Stratford-Upon-Avon
Posts: 137
Thanks: 3
maeltar is on a distinguished road
Default

WoW you are doing well for your age Salathe

Quote:
Originally Posted by Salathe View Post
For example:

PHP Code:
$dob '1066-12-03'
__________________
Thanks... Simon

Sex, Drugs & Linux Rules
Send a message via MSN to maeltar
maeltar is offline  
Reply With Quote
Old 01-08-2011, 01:31 PM   #12 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by maeltar View Post
WoW you are doing well for your age Salathe
Haha, I'm not that old!
Salathe is offline  
Reply With Quote
Old 01-10-2011, 07:19 AM   #13 (permalink)
The Visitor
 
Join Date: Jan 2011
Location: india
Posts: 4
Thanks: 0
mojo19 is on a distinguished road
Smile Hi

Thanks for all of you who just posted a better way of doing that.
I am just a beginner and need guidance in solving things in an easier way.
Hope all of you will support.
mojo19 is offline  
Reply With Quote
Old 01-12-2011, 02:18 AM   #14 (permalink)
The Visitor
 
hitchy's Avatar
 
Join Date: Jan 2011
Location: New Zealand
Posts: 3
Thanks: 0
hitchy is on a distinguished road
Default

i tested this code
Quote:
$dob = '1066-12-03';
$age = date_diff(date_create($dob), date_create('now'))->y;
i changed the date to 01-13-1982(making someone 28). i tried different styles of the date, Y-M-D, D-M-Y, M-D-Y but they all display the same thing, it dsplays as 29 NOT 28. only on 01-15-1982 dose it display the correct age.

is there something wrong with the code or am i doing something wrong?
hitchy is offline  
Reply With Quote
Old 01-12-2011, 07:41 AM   #15 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

hitchy, I'm going to guess that you're using PHP 5.3.0, .1 or .2? There is a bug in those versions which affects diffing a date in January. It is fixed in 5.3.3 for what that's worth.
Salathe is offline  
Reply With Quote
Old 04-14-2012, 02:23 PM   #16 (permalink)
The Wanderer
 
ibndawood's Avatar
 
Join Date: Dec 2007
Location: Chennai, India
Posts: 11
Thanks: 11
ibndawood is on a distinguished road
Default

PHP Code:
$birthday_date '1985-10-06';
$current_date date('Y-m-d'); //today is 2011-10-04
$diff_in_mill_seconds strtotime($current_date) - strtotime($birthday_date);
$age floor($diff_in_mill_seconds / (365.2425 *60*60*24)) + 1//365.2425 is the no. of days in a year. 
I have written about this here : http://ibndawood.com/2011/10/calcula...eir-birthdate/ and also a problem i faced if you were to use 365 instead of 365.2425
ibndawood is offline  
Reply With Quote
Old 06-25-2012, 12:36 PM   #17 (permalink)
The Wanderer
 
Join Date: May 2012
Location: usa
Posts: 6
Thanks: 0
cheapcnjerseys is on a distinguished road
Default

Nike NFL Jerseys from china, Nike NFL Jerseys online,cheap Nike NFL Jerseys,2012 nike nfl jerseys, nike nfl women jerseys,buy jerseys online, soccer jerseys shop online, nfl jerseys store online
cheapcnjerseys 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 06:11 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design