# Thread: Calculating an age function.

1. ## Calculating an age function.

Just to contribute to the growth of PHP and all that, I've made a script that can calculate the age of someone based on what day, month(in text format) and year they submit.

The function gets the values from the drop down boxes (or input boxes if you wish) and then calculates what month they submitted by changing the month into a numerical value (E.g. April becomes 04).

The Function - This can go at the top of the script or whatever

PHP Code:
``` function getAgeByDate(\$iMonth, \$iDay, \$iYear) {     \$iTimeStamp = (mktime() - 86400) - mktime(0, 0, 0, \$iMonth, \$iDay, \$iYear);     \$iDays = \$iTimeStamp / 86400;     \$iYears = floor(\$iDays / 365.25);     return \$iYears; } //converts a text month to its corresponding number value function monthtonumber(\$month) {     switch (\$month) {     case "January":     \$nomonth = "01";     break;     case "February":     \$nomonth = "02";     break;     case "March":     \$nomonth = "03";     break;     case "April":     \$nomonth = "04";     break;     case "May":     \$nomonth = "05";     break;     case "June":     \$nomonth = "06";     break;     case "July":     \$nomonth = "07";     break;     case "August":     \$nomonth = "08";     break;     case "September":     \$nomonth = "09";     break;     case "October":     \$nomonth = "10";     break;     case "November":     \$nomonth = "11";     break;     case "December":     \$nomonth = "12";     break; } return \$nomonth; }  ```
And to get the value of the age, call it using this:

PHP Code:
``` \$birthmonth = monthtonumber(\$birthmonth); \$age = getAgeByDate(\$birthmonth, \$birthday, \$birthyear);  ```
You could say I made this out of boredum, but I thought i'd share it for future people needing something like this.

2. Thanks James, good to see I'm not the only bored programmer out there

3. round function would wrk better than floor

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•