views:

43

answers:

2

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.

+1  A: 

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;
}
xil3
Maybe I'm doing something wrong but doesn't works
CIRK
Sorry, I modified it - should work now.
xil3
Hmm as I see it works, but I test it a little before I accept it ;)
CIRK
Thanks very much!!
CIRK
A: 

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
Sarfraz
There is no 'DateDiff' function...You could instantiate DateTime twice (with the current date and birth date), and then call it's diff method.My point being, that you weren't very helpful/specific...
xil3
@xil3: It is a custom function actually.
Sarfraz
@xil3 You're right, there is no built-in function named `DateDiff`, that's why he linked to it.
Tim Cooper
I've copied the function but everything is commented out lol..
CIRK
@sAc My bad - I stand corrected! Didn't notice it was a link haha...
xil3