GetDotted Domains

Viewing Thread:
"Calculating age with PHP"

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.

Mon 02/02/09 at 00:09
Regular
"wheres the Any Key?"
Posts: 161
Hiya, ive got a prob which im sure is simple to sort, i'm trying to calculate a persons age from a given date of birth, but the code returns an incorrect age if the day and month is in the future.

// 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.
Mon 02/02/09 at 01:16
Regular
"wheres the Any Key?"
Posts: 161
Thx 4 the help Pi

Works a treat and much appreciated.

Keep safe m8.
Mon 02/02/09 at 00:46
Regular
"But less irrational"
Posts: 13
You need to check for two cases:

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.
Mon 02/02/09 at 00:09
Regular
"wheres the Any Key?"
Posts: 161
Hiya, ive got a prob which im sure is simple to sort, i'm trying to calculate a persons age from a given date of birth, but the code returns an incorrect age if the day and month is in the future.

// 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.

Freeola & GetDotted are rated 5 Stars

Check out some of our customer reviews below:

Many thanks!
You were 100% right - great support!
Thanks!
Thank you for dealing with this so promptly it's nice having a service provider that offers a good service, rare to find nowadays.

View More Reviews

Need some help? Give us a call on 01376 55 60 60

Go to Support Centre
Feedback Close Feedback

It appears you are using an old browser, as such, some parts of the Freeola and Getdotted site will not work as intended. Using the latest version of your browser, or another browser such as Google Chrome, Mozilla Firefox, or Opera will provide a better, safer browsing experience for you.