Php 如何从生日轻松确定年龄?(菲律宾)
可能重复:Php 如何从生日轻松确定年龄?(菲律宾),php,mysql,date,Php,Mysql,Date,可能重复: 嗨 我有一张桌子,上面有一个代表生日的字段。我如何找到从那个日期算起的人的年龄 这就是我所拥有的 $qPersoonsgegevens = "SELECT * FROM alg_persoonsgegevens WHERE alg_persoonsgegevens_leerling_ID = $leerling_id"; $rPersoonsgegevens = mysql_query($qPersoonsgegevens); $aPersoonsgegevens = mysq
嗨 我有一张桌子,上面有一个代表生日的字段。我如何找到从那个日期算起的人的年龄 这就是我所拥有的
$qPersoonsgegevens = "SELECT * FROM alg_persoonsgegevens WHERE
alg_persoonsgegevens_leerling_ID = $leerling_id";
$rPersoonsgegevens = mysql_query($qPersoonsgegevens);
$aPersoonsgegevens = mysql_fetch_assoc( $rPersoonsgegevens );
$timeBirthdate = mktime($aPersoonsgegevens['alg_persoonsgegevens_geboortedatum']);
不幸的是,我不知道如何从这一点出发来获得年龄
非常感谢您的帮助。Matthy试试这个:
function getAge($then) {
$then = date('Ymd', strtotime($then));
$diff = date('Ymd') - $then;
return substr($diff, 0, -4);
}
这样说吧:
$age = getAge($aPersoonsgegevens['alg_persoonsgegevens_geboortedatum']);
生日和年龄取决于文化
记住,有些文化从受孕开始计算你的年龄,或者从出生时1岁开始计算,这可能是有用的。特别是,在计算年龄时,您需要小心韩国市场
我怀疑问题的作者是否需要这些信息,但我只是想大声说出来,因为它可能在某个地方或某个时候对程序员有用。我编写了一个应用程序,以月或日(1个月以下的婴儿)显示婴儿的年龄(最多24个月): 因此,您可能有这些人类可读的年龄值:
- 二十一,
- 三,
- 17个月
- 三天
public function getAge ()
{
if ($this->getBirthDate() === null) {
return null;
}
if (! $this->getAdmitTime() == null ) {
$relative_to = $this->getAdmitTime(null); # null format returns Unix ts
}
else {
$relative_to = time(); # Unix ts
}
$age_in_seconds = $relative_to - $this->getBirthDate(null);
$age_in_years = intval($age_in_seconds / (60*60*24*365.25));
if ($age_in_years >= 2) {
return $age_in_years;
}
else {
$age_in_months = intval($age_in_seconds / (60*60*24*30));
if ($age_in_months >= 1) {
return "$age_in_months month". ($age_in_months == 1 ? '' : 's');
}
else {
$age_in_days = intval($age_in_seconds / (60*60*24));
return "$age_in_days day" . ($age_in_days == 1 ? '' : 's');
}
}
}