根据出生日期计算年龄

方式1

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

方式2

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

方式3

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

SELECT TIMESTAMPDIFF(YEAR, from_unixtime(birthday), CURDATE()) AS age from account where id = 10; 

相关资料

根据出生日期计算年龄

发表评论