开发者

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

开发者 https://www.devze.com 2023-01-06 09:47 出处:网络
I have 3 variables $day, $month, $year each of them have the values what the users given to them. I want to get his real Age too from these 3 variables.

I have 3 variables $day, $month, $year each of them have the values what the users given to them.

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


02, 07, 1990 -> Now his age is 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. 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
0

精彩评论

暂无评论...
验证码 换一张
取 消