根据出生日期计算年龄

根据出生日期计算年龄

问题描述:

我在 sql 中有一个用户表,他们每个人都有出生日期.我想将他们的出生日期转换为他们的年龄(仅限年),例如日期:15.03.1999 年龄:14 和 15.03.2014 将更改为年龄:15

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']}";
}

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;

演示

函数:date_create()date_diff()

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

演示

函数:TIMESTAMPDIFF(), CURDATE()