Click to See Complete Forum and Search --> : another DateDIff question


kproc
07-26-2006, 09:18 PM
Hi

My code works but retuns an unformated value

age returns a value of 728559

Birth date was April 17 2001

how do I convert that to an age


<?
function dateDiff($dformat, $endDate, $beginDate)
{
$date_parts1=explode($dformat, $beginDate);
$date_parts2=explode($dformat, $endDate);
$start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);
$end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);
return $end_date - $start_date;
}

?>




$age = dateDiff("/", $dob,date('m/d/y'));

bokeh
07-27-2006, 05:22 AM
Your two major problem are:
not using a capital "Y" in your call to the date function (lower case "y" returns "06" which corresponds to 6AD)
arguments 2 and 3 when calling your user defined function are in the wrong order
You also need to take leap years into consideration. Maybe something like this: function age($dob /* format m/d/Y */, $delimiter ='/')
{
if(!function_exists('is_leap'))
{
function is_leap($year)
{
return (($year%4)?false:(($year%400)?true:false));
}
}
list($month, $day, $year) = explode($delimiter, $dob);
$days = gregoriantojd($m = date('m'), date('d'), $Y = date('Y'))
- gregoriantojd($month, $day, $year);
$age = 0;
while($days >= ($remove = (is_leap((($m < 3))?--$Y:$Y--) ? 366 : 365)))
{
$days -= $remove;
$age++;
}
return $age;
}

// test it
echo age('04/17/2001');