@kprocauthorAug 30.2006 — #Below is a function that I fould which is suppose to calculate a persons age. The persons date of birth comes from a mysql database (column set as Date) in the format yyyy-mm-d. When the mysql column was set a VARCHAR and I entered the date as MM/DD/YYYY the below worked.
and ideas why this is not working
the function [code=php] <? 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; }
?>
[/code]
calculating the age [code=php] $spouseage = dateDiff("-", date("Y-m-d"), $spousedob); [/code]
@kprocauthorAug 30.2006 — #As you can tell I'm by far not an expert at this. I found the function I posted on by seaching google. why would someone use such a compliated proccess when you can do it as you posted above
for the var $birthDate you would set that to what is in the database, or where ever your getting the source from.[/QUOTE] Try that with at birthdate of 1986-12-31, and you'll find that it gives the same age, even though the person hasn't had his/her birthday yet for this year. That is what the more complicated function you were looking at is trying to deal with.
(The above function is actually just subtracting the year portions of each value, as anything in the strings from the first non-numeric character (the hyphen) will be ignored when treating strings as numbers.)
@NogDogAug 30.2006 — #The gregoriantojd() function wants its arguments in this order: month, day, year. So if your dates are in y-m-d (which is much better, in my opinion), then you'll need to change these two lines: [code=php] $start_date=gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]); $end_date=gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]); [/code]