hi, i need to calculate the age from mysql date format.

I found a way to do that but its not accurate. e.g dob is 1st may 1987 and it calculate it as 21yrs old. it shu be 20+ as the acutal month of birthday is not over.

here is my code:

function datediff($dformat, $endDate, $beginDate)
	{
		$date_parts1=explode($dformat, $beginDate);
		$date_parts2=explode($dformat, $endDate);
		$start_date=gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
		$end_date=gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);

	return $end_date - $start_date;
}
$dob='1987-05-01';
round(datediff("-", date("Y-m-d", time()), $dob)/365, 0)

any idea guys??

    Why not just let the DB do the calculation:

    SELECT
    (YEAR(CURDATE()) - YEAR(dob_col) - IF(MONTH(CURDATE()) <= MONTH(dob_col) && DAY(CURDATE()) <= DAY(dob_col), 1, 0)) AS age
    FROM myTable

      oh well i dont wanna do another sql select statement just for that. i was thinkin of doing it in a function and it can be reused later on..

        nuttynibbles wrote:

        oh well i dont wanna do another sql select statement just for that. i was thinkin of doing it in a function and it can be reused later on..

        Well...
        Here you go...

        <?php
        function getAge($timestamp)
        {
        $Age=date('Y')-date('Y',$timestamp);
        if(date('m')<date('m',$timestamp)||(date('m')==date('m',$timestamp)&&date('d')>date('d',$timestamp)))
        $Age--;
        
        return $Age;
        }
        ?>

        First message here I hope that I'll be helpful...

          hmm skletter tks for the code, but it returns me a much older age =)

            Write a Reply...