小编典典

根据出生日期计算年龄

mysql

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

在这里,我想显示用户的日期:

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

阅读 328

收藏
2020-05-17

共1个答案

小编典典

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**](https://eval.in/84187)

功能:date_create()date_diff()


MySQL > = 5.0.0

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

[**demo**](http://sqlfiddle.com/#!2/d41d8/23465)

功能:TIMESTAMPDIFF()CURDATE()

2020-05-17