 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
09-21-2007, 03:11 PM
|
#1 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
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.
|
|
|
|
The Following User Says Thank You to Wildhoney For This Useful Post:
|
|
09-21-2007, 07:14 PM
|
#2 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
you could just do this
PHP Code:
function bday($day) { $age = time() - $bday; $years = date("Y",$age); return $years; }
|
|
|
|
09-21-2007, 07:27 PM
|
#3 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
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.
|
|
|
09-21-2007, 09:58 PM
|
#4 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
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 < 0 || ($iDiffMonth == 0 && $iDiffDay < 0))
{
$iDiffYear--;
}
return $iDiffYear;
}
|
|
|
|
|
The Following User Says Thank You to Salathe For This Useful Post:
|
|
01-04-2011, 10:34 AM
|
#5 (permalink)
|
|
The Visitor
Join Date: Jan 2011
Location: india
Posts: 4
Thanks: 0
|
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.";
?>
|
|
|
|
01-04-2011, 04:43 PM
|
#6 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
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)?
|
|
|
|
01-07-2011, 05:49 AM
|
#7 (permalink)
|
|
The Visitor
Join Date: Jan 2011
Location: india
Posts: 4
Thanks: 0
|
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".
|
|
|
|
01-08-2011, 12:39 AM
|
#8 (permalink)
|
|
The Acquainted
Join Date: Nov 2007
Posts: 166
Thanks: 0
|
Quote:
Originally Posted by Village Idiot
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
|
|
|
|
01-08-2011, 07:38 AM
|
#9 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
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
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.
|
|
|
|
01-08-2011, 11:51 AM
|
#10 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
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;
|
|
|
|
01-08-2011, 11:54 AM
|
#11 (permalink)
|
|
The Acquainted
Join Date: Nov 2009
Location: nr Stratford-Upon-Avon
Posts: 137
Thanks: 3
|
WoW you are doing well for your age Salathe
Quote:
Originally Posted by Salathe
For example:
PHP Code:
$dob = '1066-12-03';
|
__________________
Thanks... Simon
Sex, Drugs & Linux Rules
|
|
|
01-08-2011, 01:31 PM
|
#12 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
Quote:
Originally Posted by maeltar
WoW you are doing well for your age Salathe
|
Haha, I'm not that old! 
|
|
|
|
01-10-2011, 07:19 AM
|
#13 (permalink)
|
|
The Visitor
Join Date: Jan 2011
Location: india
Posts: 4
Thanks: 0
|
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.
|
|
|
|
01-12-2011, 02:18 AM
|
#14 (permalink)
|
|
The Visitor
Join Date: Jan 2011
Location: New Zealand
Posts: 3
Thanks: 0
|
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?
|
|
|
|
01-12-2011, 07:41 AM
|
#15 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
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.
|
|
|
|
04-14-2012, 02:23 PM
|
#16 (permalink)
|
|
The Wanderer
Join Date: Dec 2007
Location: Chennai, India
Posts: 11
Thanks: 11
|
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
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|