Home » How To Calculate Age From Birthdate

How To Calculate Age From Birthdate

Last updated on June 9, 2020 by

Table of Contents
1. Using Functions in PHP
2. Using Carbon In Laravel
3. Using Functions In MySQL

In this article, we will teach you how to calculate age from birthdate in PHP, MySQL, and Laravel using different functions and Carbon package. Let’s see how to do it.

01 Using Functions in PHP

We can easily get the age from the Birthdate using the DateTime class in PHP. For that, we just need to create two dates, like one is Birthdate ($birthDate = new DateTime('1992-05-25')) and the second is the current date ($currentDate = new DateTime('today')), then we can use diff() function to get the difference of the two dates and fetch year from that difference.

<?php

/*@ Get age from birthdate using DateTime class */
$birthDate   = new DateTime('1992-05-25');
$currentDate = new DateTime('today');

echo $birthDate->diff($currentDate)->y;

Alternate Solution:

Alternatively, you can extract two years from two dates and get the difference between those 2 years, which will give result in the age as shown below.

<?php

$birthDate = '1995-05-25'; // Your birthdate

$currentYear = date('Y'); // Current Year

$birthYear = date('Y', strtotime($birthDate)); // Extracted Birth Year using strtotime and date() function

$age = $currentYear - $birthYear; // Current year minus birthyear

echo $age;

02 Using Carbon In Laravel

The Carbon is a very useful package in Laravel to do date-related specific tasks. You can import Carbon class like use Carbon\Carbon into your model or controller class and then you can use it anywhere. You can also calculate age using carbon as below:

<?php

$birthDate = '1992-05-25'; // Your birthdate

$age = \Carbon\Carbon::parse($birthDate)->age;

Output:

28  // This output is based on the date when this post had written

Alternate Solution:

Alternatively, you can create Accessors to get the age every time when the birthdate is being retrieved from the database.

1. Import Carbon in your model as show below:

use Carbon\Carbon;

2. Let’s now create an age Accessors as shown:

public function getAgeAttribute()
{
    return Carbon::parse($this->attributes['birthdate'])->age;
}

Don’t forget to change the database column “birthdate” to yours in above code snippet.

3. Now, It’s time use the Accessors, you can use it anywhere like in Controllers, Models or Blade views.

$user->age; // Your Collection object -> age

03 Using Functions In MySQL

We can use MySQL’s TIMESTAMPDIFF() function to get the age from the date. It takes 3 arguments format, first date, and second date. TIMESTAMPDIFF() is used to get the difference of 2 dates in a particular format like YEAR or MONTH.

Let’s suppose, you have a birth_date column into your users table then you can get the age of users as shown:

SELECT TIMESTAMPDIFF(YEAR, birth_date, CURDATE()) AS age FROM `users`

Output:

age
---
28
25
30

That’s it from our end. We hope this article helped you to learn how to calculate age from birthdate.

Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you for reading this post 🙂 Keep Smiling! Happy Coding!

 
 

Leave a Comment