How to convert DOB timestamp from mysql table into age using PHP

Hi

I have members DOB stored in a mysql table field called birthday in the following format YYYY-MM-DD

I can’t seem to convert the time stamp into the members age

I need to be able to feed $birthday which is in the above format and then use php code to convert to age

I haven’t tested this but something like this should do this trick
[php]
$dob = … however you get dob …;
$dob_time = strtotime($dob);//convert birthday into a unix timestamp
$now = time();//get current unix time stamp

$difference = $now - $dob_time;//find difference between timestamps

$year_in_secs = 606024;//a year in seconds

$years = $difference / $year_in_secs;

//$years = floor($years); Floor it for display if you want
[/php]

strtotime: http://php.net/manual/en/function.strtotime.php
time: http://php.net/manual/en/function.time.php

The first bit.
[php]
//quick script to make the data look nice
function formatDate($val)
{
list($date, $time) = explode(" “, $val);
list($year, $month, $day) = explode(”-", $date);
list($hour, $minute, $second) = explode (":", $time);
return date(“l, m.j.y @ H:ia”, mktime($hour, $minute, $second, $month, $day, $year));
}
[/php]

Getting the date from the database:
[php]
$commentDate = formatDate($commentrow[6]);
[/php]

Display the date and time.
[php]

<? echo "on $commentDate"; ?>

[/php]

Hope that helps

Sponsor our Newsletter | Privacy Policy | Terms of Service