sheikhmdirfan Posted June 29, 2009 Share Posted June 29, 2009 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-02-2008.. then it should show his/her age as 1 year and 4 months taking into consideration the number of days of each month.. Since all months dont have equal number of days.. How can i calculate his age in terms of years and number of months.. Any help is highly appreciated.. Thanks in advance.. Quote Link to comment https://forums.phpfreaks.com/topic/164058-calculating-age-in-number-of-years-and-number-of-months/ Share on other sites More sharing options...
Twister1004 Posted June 29, 2009 Share Posted June 29, 2009 Have you tried starting on this? If you have post your code. However, I would use the year it is now minus their birth year. Quote Link to comment https://forums.phpfreaks.com/topic/164058-calculating-age-in-number-of-years-and-number-of-months/#findComment-865441 Share on other sites More sharing options...
sasa Posted June 29, 2009 Share Posted June 29, 2009 <?php function my_old($dob, $now = false){ if (!$now) $now = date('d-m-Y'); $dob = explode('-', $dob); $now = explode('-', $now); $old = $now[2]*12+$now[1]-$dob[2]*12-$dob[1]-($dob[0]>$now[0] ? 1 : 0); return array('year' => floor($old / 12), 'mnt' => $old % 12); } $x = my_old('29-4-2008', '29-4-2009'); print_r($x); ?> Quote Link to comment https://forums.phpfreaks.com/topic/164058-calculating-age-in-number-of-years-and-number-of-months/#findComment-865449 Share on other sites More sharing options...
pacchiee Posted August 25, 2009 Share Posted August 25, 2009 Can you please add the number of days too? Something like this : 2 Years 4 Months 12 Days Thanks in Advance! Quote Link to comment https://forums.phpfreaks.com/topic/164058-calculating-age-in-number-of-years-and-number-of-months/#findComment-905815 Share on other sites More sharing options...
sasa Posted September 2, 2009 Share Posted September 2, 2009 <?php function my_old($dob, $now = false){ if (!$now) $now = date('d-m-Y'); $dob = explode('-', $dob); $now = explode('-', $now); $mnt = array(1 => 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); if (($now[2]%400 == 0) or ($now[2]%4==0 and $now[2]%100!=0)) $mnt[2]=29; if($now[0] < $dob[0]){ $now[0] += $mnt[$now[1]-1]; $now[1]--; } if($now[1] < $dob[1]){ $now[1] += 12; $now[2]--; } if($now[2] < $dob[2]) return false; return array('year' => $now[2] - $dob[2], 'mnt' => $now[1] - $dob[1], 'day' => $now[0] - $dob[0]); } $x = my_old('29-4-2008', '28-5-2009'); print_r($x); ?> Quote Link to comment https://forums.phpfreaks.com/topic/164058-calculating-age-in-number-of-years-and-number-of-months/#findComment-910776 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.