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.
Additionally, read our guide:
- How to Select Data Between Two Dates in MySQL
- Error After php artisan config:cache In Laravel
- Specified Key Was Too Long Error In Laravel
- AJAX PHP Post Request With Example
- How To Use The Laravel Soft Delete
- How To Add Laravel Next Prev Pagination
- cURL error 60: SSL certificate problem: unable to get local issuer certificate
- Difference Between Factory And Seeders In Laravel
- Laravel: Increase Quantity If Product Already Exists In Cart
- How to Convert Base64 to Image in PHP
- Check If A String Contains A Specific Word In PHP
- Dynamically Populate A Select Field’s Choices In ACF
- How To Find Duplicate Records in Database
- How To Create Dynamic Variable In PHP
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!
Rattling nice design and style and great subject material , hardly anything else we want : D. Terza Clair Ansilme