For fun, I decided to write a simple PHP script that would give you your age accurately (in years) regardless of what point in the current year you’re at. You provide the digits of your birthday (month, day, and year) and it processes a few arguments (whether the birth year provided is valid and whether you’ve had your birthday this year yet) and provides you the correct age in years. This is it in action (using my birth date):
User is 42 years old!
So, yeah… it’s a real basic script. However, where it really ends up being useful is when you manually include your age (in years) on your website. Once your next birthday occurs, the age you manually typed in is no longer valid which means you need to manually update it each year. Why not have a script dynamically update it for you? You need only include the file within your php page like so (with the correct link to the age file):
include ("linktoyouragefile.php");
If you’d like to look at the code itself, the code in its entirety follows below:
// Your Age // Coded by K. Enders // User variables -- only change values in this section! // Provide the digit values for your birthday below $bdaymonth = "8"; $bdayday = "27"; $bdayyear = "1982"; // Now let the script do all the work! // grabbing the current date info $currentmonth = date("n"); $currentday = date("j"); $currentyear = date("Y"); // first, make sure that the year provided is not the current one or one from the future if($bdayyear >= $currentyear) { echo "Please enter a valid year!"; } else { // now check to see if the current month is before or after the user's birth month if($currentmonth > $bdaymonth) { // if the current month is higher than the birth month, it means the user has already had their birthday this year... go ahead and do the math $user_age = $currentyear - $bdayyear; echo "User is $user_age years old!"; } elseif($currentmonth == $bdaymonth) { // it is the user's birth month -- we need to figure out if they've already reached their birth 'day' this year or not // figure this out by comparing current day to birth day if($currentday > $bdayday) { // user's bday has occurred, do the math $user_age = $currentyear - $bdayyear; echo "User is $user_age years old!"; } elseif ($currentday == $bdayday) { // user's bday is today, do the math $user_age = $currentyear - $bdayyear; echo "User is $user_age years old!"; } elseif ($currentday < $bdayday) { // user's birthday hasn't occured, do the adjusted math $user_age = $currentyear - $bdayyear; $adjusted_user_age = $user_age - 1; echo "User is $adjusted_user_age years old!"; } // end day check } elseif($currentmonth < $bdaymonth) { // user hasn't reached their birthday this year so you can do the math and minus 1 to keep their actual age correct $user_age = $currentyear - $bdayyear; $adjusted_user_age = $user_age - 1; echo "User is $adjusted_user_age years old!"; } // end month check } // end for year check
Using it for the aforementioned purpose, I would, of course, change ‘User is x years old’ to something like ‘x years old’ and integrate that into your site.
Not Quite Done
It was fun writing the first version, but I wanted a more front-end user friendly version as well. So, I decided I wanted to add a front end that allows the user to put in their birth date and receive their age in years. It was a lot easier than I expected. 🙂 I’m including a screenshot of it in action below:
I added a third variation of the script that uses javascript alert boxes to deliver the age. The results look like this:
If you would like to download the script (containing all 3 variations of the script), click the Download button below:
I like the efforts you have put in this, regards for all the great blog posts.
Hello to all, how iss the wһole thіng, І thіnk еνery one is ցetting moгe from thіѕ site, and your views are good ffor neᴡ people.