fruzle.com logo


Class B no-www status

Get Firefox
 

Home » Various tutorials » Calculate your age in PHP


Calculate your age in PHP 

Code added since previous step is in blue.
Comments are in green (you can remove those from your code if you want).


There's a very simple way to calculate and display your age using PHP, and here's how.

Start off defining your birthday in segments:
<?php
  $bdayYear = "1988"; // the year you were born
  $bdayMonth = "04"; // the month in which you were born
  $bdayDay = "15"; // the day you were born
?>

Don't forget to enter the month and day as a two digit number, and the year as a four digit number.

Now let's add the current date's info:
<?php
  $bdayYear = "1988"; // the year you were born
  $bdayMonth = "04"; // the month in which you were born
  $bdayDay = "15"; // the day you were born
  
  $currentYear = date(Y); // the current year
  $currentMonth = date(m); // the current month
  $currentDay = date(d); // the current day
?>


Before we can calculate your current age, we're going to calculate the difference between the current year and your birth year:
<?php
  $bdayYear = "1988"; // the year you were born
  $bdayMonth = "04"; // the month in which you were born
  $bdayDay = "15"; // the day you were born
  
  $currentYear = date(Y); // the current year
  $currentMonth = date(m); // the current month
  $currentDay = date(d); // the current day
  
  $yearDiff = $currentYear - $bdayYear; // difference between current year and your birth year
?>


Since you're not the same age throughout the entire year, we need to check against today's date:
<?php
  $bdayYear = "1988"; // the year you were born
  $bdayMonth = "04"; // the month in which you were born
  $bdayDay = "15"; // the day you were born
  
  $currentYear = date(Y); // the current year
  $currentMonth = date(m); // the current month
  $currentDay = date(d); // the current day
  
  $yearDiff = $currentYear - $bdayYear; // difference between current year and your birth year
  
  if (($currentMonth < $bdayMonth) || ($currentMonth = $bdayMonth && $currentDay < $bdayDay)) :
?>

This new line basically says:
if the current month's number is smaller than that of the birthday month, or (represented by the ||) if the current month equals the birthday month and at the same time the current day's number is smaller than that of the birthday's day, then....


Well, then it hasn't been your birthday yet, and the difference between the current year and your birth year is one too high, so let's fix that:
<?php
  $bdayYear = "1988"; // the year you were born
  $bdayMonth = "04"; // the month in which you were born
  $bdayDay = "15"; // the day you were born
  
  $currentYear = date(Y); // the current year
  $currentMonth = date(m); // the current month
  $currentDay = date(d); // the current day
  
  $yearDiff = $currentYear - $bdayYear; // difference between current year and your birth year
  
  if (($currentMonth < $bdayMonth) || ($currentMonth = $bdayMonth && $currentDay < $bdayDay)) :
    $age = $yearDiff - 1;
  
?>


But what if it has been your birthday? Then the difference between the current year and your birthyear is the same as your age:
<?php
  $bdayYear = "1988"; // the year you were born
  $bdayMonth = "04"; // the month in which you were born
  $bdayDay = "15"; // the day you were born
  
  $currentYear = date(Y); // the current year
  $currentMonth = date(m); // the current month
  $currentDay = date(d); // the current day
  
  $yearDiff = $currentYear - $bdayYear; // difference between current year and your birth year
  
  if (($currentMonth < $bdayMonth) || ($currentMonth = $bdayMonth && $currentDay < $bdayDay)) :
    $age = $yearDiff - 1;
  else:
    $age = $yearDiff;
?>


To show if today is your birthday, we need to add some more code:
<?php
  $bdayYear = "1988"; // the year you were born
  $bdayMonth = "04"; // the month in which you were born
  $bdayDay = "15"; // the day you were born
  
  $currentYear = date(Y); // the current year
  $currentMonth = date(m); // the current month
  $currentDay = date(d); // the current day
  
  $yearDiff = $currentYear - $bdayYear; // difference between current year and your birth year
  
  if (($currentMonth < $bdayMonth) || ($currentMonth = $bdayMonth && $currentDay < $bdayDay)) :
    $age = $yearDiff - 1;
    $bday = " (".$bdayMonth."/".$bdayDay."/".$bdayYear.")";
  elseif (($currentMonth = $bdayMonth && $currentDay = bdayDay)) :
    $age = $yearDiff;
    $bday = " today!";
  else:
    $age = $yearDiff;
    $bday = " (".$bdayMonth."/".$bdayDay."/".$bdayYear.")";
  endif;
?>

As you can see, I added a new variable to the three parts of the statement: $age.
You can leave that variable out if you don't want to have it say your actual birthday.


So how do you get your age on a web page?
Place the code from above in the source code and add any of the following lines where you want the information shown. Note that the codes shown are for when you put them in regular HTML blocks rather than an echo-ed PHP block.

to show only the age:
<?php echo $age; ?>

to show only your birthday:
<?php echo $bday; ?>

to show both your age and birthday:
<?php echo $age . $bday; ?>


Need more explanation? Found a typo? Contact me.