The "Freeola Customer Forum" forum, which includes Retro Game Reviews, has been archived and is now read-only. You cannot post here or create a new thread or review on this forum.
// dob 6/3/1970
$day=6;
$month=3;
$year=1970;
// calculate age
$age=date("Y") - $year;
// correct age if month & day is in future
if(date("n") < $month & date("j") < $day)
{
$age=$age-1;
}
echo $age;
This code is displaying the result as
39
it should be
38
it must be a simple error but im new to this and cant see it for looking. Any ideas?
many thx in advance.
// dob 6/3/1970
$day=6;
$month=3;
$year=1970;
// calculate age
$age=date("Y") - $year;
// correct age if month & day is in future
if(date("n") < $month & date("j") < $day)
{
$age=$age-1;
}
echo $age;
This code is displaying the result as
39
it should be
38
it must be a simple error but im new to this and cant see it for looking. Any ideas?
many thx in advance.
1) the month is earlier
OR
2) the month is the same and the day is earlier
i.e.
if(date("n") < $month || (date("n") == $month && date("j") < $day)) {
$age = $age - 1;
}
At present, a later or equal day in an earlier month won't trigger the condition.
Also, be careful with AND and OR operators, as there are two of each.
A single ampersand, &, is used for bitwise AND, but two ampersands, &&, are used for logical AND. Ditto, for bitwise OR, |, and logical OR, ||.
Logical AND and OR are used when stringing conditions together.
Bitwise AND and OR are used to turn individual bits in a variable on and off.
Works a treat and much appreciated.
Keep safe m8.