Calculating age in years and number of months

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sheikhmdirfan
Forum Newbie
Posts: 22
Joined: Tue Jan 20, 2009 12:21 am

Calculating age in years and number of months

Post 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..
User avatar
juma929
Forum Commoner
Posts: 72
Joined: Wed Jun 17, 2009 9:41 am

Re: Calculating age in years and number of months

Post 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 :D.

Thanks.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Calculating age in years and number of months

Post by pickle »

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.
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Re: Calculating age in years and number of months

Post 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
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Calculating age in years and number of months

Post 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;
}
?>
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Re: Calculating age in years and number of months

Post by a94060 »

sorry about my code, it doesnt take into account months having 28,30, or 31 days. It doesnt account for that diffrence
Post Reply