How To Calculate Age from Date of Birth in PHP Tutorial

Share this Article
Reading Time: 3 minutes
2,354 Views

Inside this article we will see the concept to calculate age from date of birth in php. Article contains classified information about calculating age from current date in php.

If you are looking for an article which gives you the understanding to calculate age from current date then this is best to learn and get the concept.

You will see how to calculate age from current date in php in two different examples. Also you can make it as a age calculator application in PHP. You need to take age from user as an input and calculate user age with this concept.

Learn More –

Let’s get started.


Example #1: Using date() & strtotime() PHP Functions

Create a file index.php at your localhost directory.

Open index.php and write this complete code into it.

<?php

$dob = '1992-06-01';
$year = (date('Y') - date('Y', strtotime($dob)));

echo $year;

Output

29

Example #2: Using DateTime() PHP Class

Create a file index.php at your localhost directory.

Open index.php and write this complete code into it.

<?php

$dob = new DateTime('1992-06-01');
$today   = new DateTime('today');

$year = $dob->diff($today)->y;

echo $year;

Output

29

We hope this article helped you to learn How To Calculate Age from Date of Birth in PHP Tutorial in a very detailed way.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Was this post helpful?

PHP Web Development Courses on Offer in just $4
PHP Web Development Courses on Offer in just $4