How to Calculate Age from Date of Birth in PHP

Today we are going to learn a very interesting topic. As the title says that we are going to learn that “Calculate Age from Date of Birth in PHP” or simply we are going to learn That “How old are you?” Yes, you can use PHP as your birthday calculator too. But the aim of remembering PHP dates function is not designed to calculate your birth date, it is designed to solve common problems that occur in our life regarding date and time. There are several tutorials on the web but I tried to make it as simple as I could. I myself, sometimes want to calculate my age for some reason. So this tutorial will help you a lot in your future, I think.

Problem About Calculating Age

Suppose you developed a website or web application and you don’t want to add peoples whose age is below 18 years. In this case, this script will help you to compute his/her age from birthdate to restrict them to enter the site. In other cases, you can calculate the number of days passed from a specific date. You also can calculate the number of days between two dates.

Solution for Age Calculation in PHP

Here I am writing a short PHP code snippet to calculate age from date of birth by subtracting days for the current date or given date. However, in our example, we are using the current date. Please read the below code and try to understand. Copy-paste this code into any IDE or write it and execute it to see the result. However, at the end of this article, I’ll explain it.

$bdate      =   '11/15/1986';
$ageyears   =   date_diff(date_create($bdate), date_create('now'))->y;
$agemonths  =   date_diff(date_create($bdate), date_create('now'))->m;
$agedays    =   date_diff(date_create($bdate), date_create('now'))->d;
echo 'You age is '.$ageyears.' Years, '.$agemonths,' months, and '.$agedays.' days.';

How to calculate age in PHP? Explanation

Finally, we did it. In the above code, we just used two functions date_create() anddate_diff() “in MS excel, datediff function calculates dates difference” to calculate the age of the user. Looking into syntax perspective we realize that in date_diff() function we put the first date from (first or start date) and then we put date to (current date of the current year) But remember again these function are not only for calculating your age. These are widely used in problems relating to dates and times.

In the above code snippets, we first declare a variable assigns value as the date of birth. After that variable, we declared three more variables using date_diff() and date_create() method and extract Year, Months, and Days as an integer value from these functions, and at the end, we just concatenate all of them.

Here is the calculated output of the above code. It will just echo age with Years, Months, and Days as shown below:

Final Output of age calculator

With the help of the above-mentioned code snippets, you can write and complete the function for the date calculator. One thing more, these function doesn’t treat leap year, also these functions are based on Georgian calendar. For more detail, on Georgian Calenders, you can read HERE.

There are some other techniques that will help you to find leap years. We’ll discuss it in another tutorial.

If you need any kind of help then you can comment below or you can send us a message by clicking HERE.

We have some Web Applications written in PHP. If you want to take a look please visit our Point of Sale and Inventory Management System and School Management System.

One Thought on “How to Calculate Age from Date of Birth in PHP”

Leave a Reply - I will show after approval