• support@answerspoint.com

calculate age based on date of birth

1859

I have a table of users in sql and they each have birth dates. I want to convert their date of birth to their age (years only), e.g. date: 15.03.1999 age: 14 and 15.03.2014 will change to age: 15

Here I want to show the date of the user:


if(isset($_GET['id']))
{
$id = intval($_GET['id']);
    $dnn = mysql_fetch_array($dn);$dn = mysql_query('select username, email, skype, avatar, date, signup_date, gender from users where id="'.$id.'"');
    $dnn = mysql_fetch_array($dn);
    echo "{$dnn['date']}";
.
.
.

 

1Answer


0

PHP >= 5.3.0 


# object oriented
$from = new DateTime('1970-02-01');
$to   = new DateTime('today');
echo $from->diff($to)->y;

# procedural
echo date_diff(date_create('1970-02-01'), date_create('today'))->y;

demo

functions: date_create()date_diff()


MySQL >= 5.0.0

SELECT TIMESTAMPDIFF(YEAR, '1970-02-01', CURDATE()) AS age

demo

functions: TIMESTAMPDIFF()CURDATE()

  • answered 8 years ago
  • Gul Hafiz

Your Answer

    Facebook Share        
       
  • asked 8 years ago
  • viewed 1859 times
  • active 8 years ago

Best Rated Questions