Page 1 of 1
Calculating age in years and number of months
Posted: Fri Jun 26, 2009 4:32 am
by sheikhmdirfan
Hi,
I would like to know as to how i can calculate age of a person years and months.
e.g say a person's DOB is 25-09-1985.. How can i calculate his age in terms of years and number of months..
Any help is highly appreciated..
Thanks in advance..
Re: Calculating age in years and number of months
Posted: Fri Jun 26, 2009 9:04 am
by juma929
Hello,
I havnt been able to test it yet but something along the lines of:
Code: Select all
<?php
$dob = strtotime("2008/01/01");
$date_today = strtotime(date("Y/m/d"));
$agex = $date_today - $dob;
$years = floor($agex / 31556926);
$remainder = $agex % 31556926;
$months = floor($remainder / 2592000);
echo $years . ' ' . $months;
should do the trick, obviously exchange the top dob var for whatever you want.
It also could do with a nice tidy up but ill let you take over from here

.
Thanks.
Re: Calculating age in years and number of months
Posted: Fri Jun 26, 2009 2:06 pm
by pickle
That won't necessarily work if the date is before January 1, 1970
Re: Calculating age in years and number of months
Posted: Fri Jun 26, 2009 9:43 pm
by a94060
I am not sure how to implement this, but would you be able to subtract the years first, so if the dates are 5/24/2007 and 6/29/2050, you would first do 2007-2050. Then you would do 5-6. Then,if the month value is negative, you would subtract one from the difference of years,and do 12-absolute value of the months.
Im pretty sure this would get around the linux time problem
Re: Calculating age in years and number of months
Posted: Sat Jun 27, 2009 1:05 pm
by Eric!
This example from the php manual pages calculates years and days. All you would have to do is convert the days to months for the current year.
Code: Select all
<?php
function get_age($date_start, $date_end) {
$t_lived = get_timestamp($date_end) - get_timestamp($date_start);
$seconds_one_year = get_days_per_year($date_start, $date_end) * 24 * 60 * 60;
$age = array();
$age['years_exact'] = $t_lived / $seconds_one_year;
$age['years'] = floor($t_lived / $seconds_one_year);
$seconds_remaining = $t_lived % $seconds_one_year;
$age['days'] = round($seconds_remaining / (24 * 60 * 60));
return $age;
}
function get_timestamp($date) {
list($y, $m, $d) = explode('-', $date);
return mktime(0, 0, 0, $m, $d, $y);
}
function get_days_per_year($date_start, $date_end) {
list($y1) = explode('-', $date_start);
list($y2) = explode('-', $date_end);
$years_days = array();
for($y = $y1; $y <= $y2; $y++) {
$years_days[] = date('L', mktime(0, 0, 0, 1, 1, $y)) ? 366 : 365;
}
return round(array_sum($years_days) / count($years_days), 2);
}
$date_birth = '1979-10-12';
$date_now = date('Y-m-d');
$age = get_age($date_birth, $date_now);
echo '<pre>';
print_r($age);
echo '</pre>';
?>
It will display something like this:
Array
(
[years_exact] => 28.972974329491
[years] => 28
[days] => 355
)
You could now loop through the following function to subtract days off for each month until your answer goes negative then you know the number of months. I'm sure there is a more direct way to compute this, but this will get you started.
Code: Select all
function days_in_month($month, $year)
{
// calculate number of days in a month
// get next month with day=0
$days=date("j",mktime(0,0,0,$month+1,0, $year));
return $days;
}
?>
Re: Calculating age in years and number of months
Posted: Sat Jun 27, 2009 1:17 pm
by a94060
sorry about my code, it doesnt take into account months having 28,30, or 31 days. It doesnt account for that diffrence