可能重複:
How to calculate age (in years) based on Date of Birth and getDate()從出生日期得到年齡?
我想呼應從他們存儲在MySQL表的出生日期一個人的年齡。有沒有我可以放入的一段代碼?
目前它只是顯示出生日期,因爲這就是我要求它做的所有事情,但如果這是可能的,我想要一種顯示年齡的方法。
下面是users.php while循環:
<?
$platinum_set = get_platinum_user();
while ($platinum = mysql_fetch_array($platinum_set)) {
echo"
<div class=\"platinumcase\">
<a href=\"profile.php?id={$platinum['id']}\"><img width=80px height= 80px src=\"data/photos/{$platinum['id']}/_default.jpg\" class=\"boxgrid\"/></a><h58> {$platinum['first_name']} {$platinum['last_name']}</h58><br/><br/>
<h52>{$platinum['dob']}<br/>
<br/>{$platinum['location']}</h52>
</div>";
}
?>
這裏的功能:
function get_platinum_user() {
global $connection;
$query = "SELECT *
FROM ptb_users, ptb_profiles
WHERE ptb_users.account_type = \"User\"
AND ptb_users.account_status = \"Active\"
AND ptb_profiles.user_id = ptb_users.id
AND ptb_users.subscription = \"Platinum\"
LIMIT 0 , 30";
$platinum_set = mysql_query($query, $connection);
confirm_query($platinum_set);
return $platinum_set;
}
因此計算年齡。你有什麼嘗試? –
我試過這個while循環,但它不起作用,仍然顯示出生日期: –
$ dob = $ platinum ['dob']; function age_from_dob($ dob){ list($ y,$ m,$ d)= explode(' - ',$ dob); (($ m =(date('m') - $ m))<0){ $ y ++; else if($ m == 0 && date('d') - $ d <0){ $ y ++; } 返回日期('Y') - $ y; } $ dob = age_from_dob($ dob); ?> –