Jump to content

Recommended Posts

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..

<?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);
?>

  • 1 month later...
<?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);
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.