根据出生日期计算年龄 [英] Calculate age based on date of birth

查看:48
本文介绍了根据出生日期计算年龄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 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()

这篇关于根据出生日期计算年龄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆