这是我正在使用的查询,它位于第1页,不在函数内部。
<?php
$sql2= mysql_query("SELECT *
FROM catego WHERE category_id = '$idc'");
$categoryCount = mysql_num_rows($sql2);
if ($categoryCount>0 )
{
$row2 = mysql_fetch_array($sql2);
$id = $row2["category_id"];
$birthdate = $row2["birthdate"];
$address = $row2["address"];
}
?>
答案 0 :(得分:5)
SELECT YEAR(SUBDATE(NOW(), INTERVAL DATE_FORMAT("1975,09,02", "%Y-%m") YEAR_MONTH)) fs_age;
答案 1 :(得分:1)
一个简单的php函数可能类似于以下内容:
function birth_date ($birth_date){
list($y,$m,$d) = explode(",",$birth_date);
$y_diff = date("Y") - $y;
$m_diff = date("m") - $m;
$d_diff = date("d") - $d;
if ($m_diff < 0 || $d_diff < 0) { $y_diff--; }
return $y_diff;
}
即分开你的逗号,计算出差异,调整他们出生日期/月份的哪一方,如果是这样,将年份减少一年,然后返回年份。
然后在你的页面中你可以输入:
<li><span class="label">Age:</spa><?php$age = getage($birthdate); echo $age;?> </li>
修改强>:
<?php
function birth_date($birth_date){
list($y,$m,$d) = explode(",",$birth_date);
$y_diff = date("Y") - $y;
$m_diff = date("m") - $m;
$d_diff = date("d") - $d;
if ($m_diff < 0 || $d_diff < 0) { $y_diff--; }
return $y_diff;
}
$sql2= mysql_query("SELECT * FROM catego WHERE category_id = '$idc'");
$categoryCount = mysql_num_rows($sql2);
if ($categoryCount>0 )
{
$row2 = mysql_fetch_array($sql2);
$id = $row2["category_id"];
$birthdate = $row2["birthdate"];
$address = $row2["address"];
$age = birth_date($birthdate);
}
?>
答案 2 :(得分:0)
在PHP中,有几种方法可以做到这一点。这是一些很好的资源:
http://snippets.dzone.com/posts/show/1310
http://www.dreamincode.net/forums/topic/19953-calculating-age-tutorial/
答案 3 :(得分:0)
像这样,用您选择的语言:
function getAge(dateOfBirth)
{
DateTime now = DateTime.Today;
int years = now.Year - dateOfBirth.Year;
// subtract another year if we're before the birth day in the current year
if (now.Month < dateOfBirth.Month || (now.Month == dateOfBirth.Month && now.Day < dateOfBirth.Day))
--years;
return years;
}