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..
Calculating age in years and number of months
Moderator: General Moderators
-
sheikhmdirfan
- Forum Newbie
- Posts: 22
- Joined: Tue Jan 20, 2009 12:21 am
Re: Calculating age in years and number of months
Hello,
I havnt been able to test it yet but something along the lines of:
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.
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;
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
That won't necessarily work if the date is before January 1, 1970
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Re: Calculating age in years and number of months
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
Im pretty sure this would get around the linux time problem
Re: Calculating age in years and number of months
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.
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
<?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>';
?>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
sorry about my code, it doesnt take into account months having 28,30, or 31 days. It doesnt account for that diffrence