All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Calculate Age From Date Of Birth In PHP

Last Updated : Mar 11, 2024

How To Calculate Age From Date Of Birth In PHP

In this tutorial we will show you the solution of how to calculate age from date of birth in PHP, here we used three prebuilt methods of php those are ‘date(), date_diff() and date_create()’.

The date() function allows you to get the date and time from server where your php script runs.

The date_diff() function is an inbuilt method in php which is used to calculate the difference between two dates.

This function returns a dateinterval object on the success and returns false on failure. The date_create() function returns a new DateTime object.

Step By Step Guide On How To Calculate Age From Date Of Birth In PHP :-

Here we defined our own date of birth and stored to variable $dob then we collecting today date by date().

As we seen it will return date and time so here we just mentioned ‘Y-m-d’ for collecting date only from server.

We used date_create for make it our date of birth and current date into date objects then we can find difference between them easily by date_diff() method.

It returns date interval and we stored them into variable ‘$dif’ then we displaying result of only year interval from that so we mentioned ‘%y’.

<?php
$dob="24-09-1998";
$tdy=date("Y-m-d");
$dif=date_diff(date_create($dob),date_create($tdy));
echo "Your age is: ". $dif->format('%y');
?>
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
  3. Here we defined our date of birth and stored to variable ‘$dob’ then we collecting current date by using date() method.
  4. We know it returns both date and time so we just defined ‘Y-m-d’ for collects current date only with that format and stored to variable $tdy.
  5. Using date_create() method we converting them as date object and found difference between them using date_diff() method.
  6. It had value is intervals between them but we need to know year interval so we are formatting it by ‘%y’ then we gets result of our current age.
  7. Later we will use this concept with dynamic input as users wish so we get more results and this one will look like interactive webpage and more interesting when we repeatedly change date of birth and we gets correct results.

Conclusion :-

In conclusion we are able to know how to calculate age from date of birth in php.

First we need to start our xampp server then we load this program on browser we can see result of age as per we given from date of birth to current date difference.

We can also calculate them in different way by taking date of birth from user dynamically and calculating current age or you can modify the date of birth value to your own wish it won’t affect result and it will be depends on your defined date of birth.

I hope this tutorial on how to calculate age from date of birth in PHP helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪