没测试过,不知有否错误,不过思路可以用下面的方法。 <?php .... // 假设$row["birthday"]为数据库中的生日。 $birthdayArray=getDate(strtotime($row["birthday"])); //取出生日的timestamp,并且用getDate返回这个timestamp的数组。 $nowArray=getDate(); //返回当前时间的数组。$age=$nowArray["year"]-$birthdayArray["year"]; //岁数,不过不是很精确,如果需要精确的岁数,可以判断上"mon"这个数组元素。 ?> =================getDate的说明===================== getdate (PHP 3, PHP 4 >= 4.0.0)getdate -- Get date/time information Description array getdate ( [int timestamp]) Returns an associative array containing the date information of the timestamp, or the current local time if no timestamp is given, as the following array elements: "seconds" - seconds "minutes" - minutes "hours" - hours "mday" - day of the month "wday" - day of the week, numeric : from 0 as Sunday up to 6 as Saturday "mon" - month, numeric "year" - year, numeric "yday" - day of the year, numeric; i.e. "299" "weekday" - day of the week, textual, full; i.e. "Friday" "month" - month, textual, full; i.e. "January"
<?php
....
// 假设$row["birthday"]为数据库中的生日。
$birthdayArray=getDate(strtotime($row["birthday"])); //取出生日的timestamp,并且用getDate返回这个timestamp的数组。
$nowArray=getDate(); //返回当前时间的数组。$age=$nowArray["year"]-$birthdayArray["year"]; //岁数,不过不是很精确,如果需要精确的岁数,可以判断上"mon"这个数组元素。
?>
=================getDate的说明=====================
getdate
(PHP 3, PHP 4 >= 4.0.0)getdate -- Get date/time information
Description
array getdate ( [int timestamp])
Returns an associative array containing the date information of the timestamp, or the current local time if no timestamp is given, as the following array elements:
"seconds" - seconds "minutes" - minutes "hours" - hours "mday" - day of the month "wday" - day of the week, numeric : from 0 as Sunday up to 6 as Saturday "mon" - month, numeric "year" - year, numeric "yday" - day of the year, numeric; i.e. "299" "weekday" - day of the week, textual, full; i.e. "Friday" "month" - month, textual, full; i.e. "January"