简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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