我在sql中有一个用户表,他们每个都有生日。我想将他们的出生日期转换为他们的年龄(仅年),例如日期:15.03.1999年龄:14,15.03.2014并将更改为年龄:15
15.03.1999
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']}"; }
# 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()
date_create()
date_diff()
SELECT TIMESTAMPDIFF(YEAR, '1970-02-01', CURDATE()) AS age
[**demo**](http://sqlfiddle.com/#!2/d41d8/23465)
功能:TIMESTAMPDIFF(),CURDATE()
TIMESTAMPDIFF()
CURDATE()