简体   繁体   English

PHP:如何从天,月,年开始确定php的年龄?

[英]PHP: How to get age in php from day, month, year?

I have 3 variables $day, $month, $year each of them have the values what the users given to them. 我有3个变量$ day,$ month,$ year,每个变量都有用户赋予它们的值。

I want to get his real Age too from these 3 variables. 我也想从这三个变量中获得他的真实年龄。

For example the user enters this date for his birthdate in this format day,month,year: 例如,用户以以下格式输入其出生日期的日期:日,月,年:

04, 07, 1990 -> Now his age is 19 1990年4月7日->现在他的年龄是19岁


02, 07, 1990 -> Now his age is 20 1990年2月7日->现在他的年龄是20岁

I want to have it in this way. 我想要这样。

I hope it's clear. 我希望这很清楚。

Could use something like this: 可以使用如下形式:

function age($bMonth,$bDay,$bYear) {
    list($cYear, $cMonth, $cDay) = explode("-", date("Y-m-d"));
    return ( ($cMonth >= $bMonth && $cDay >= $bDay) || ($cMonth > $bMonth) ) ? $cYear - $bYear : $cYear - $bYear - 1;
}

You can use the datediff (custom) function. 您可以使用datediff (自定义)功能。 You need to subtract the birth date from current date. 您需要从当前日期减去出生日期。

Example: 例:

echo 'Now his age is ' . datediff('yyyy', '9 July 1990', '3 June 2010', false);

Result: 结果:

Now his age is 19

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM