Calculate age from date of birth in Laravel
You can easily calculate age from a given date of birth in Laravel. Laravel provides different methods on carbon class to calculate the difference between two dates, create date format and change the date format. You don’t have to create any custom function to get the age from the date of birth in Laravel.
Calculate age in years from date for birth in laravel using Carbon
//Date format Y-m-d $dateOfBirth = '1992-4-4'; $age = \Carbon\Carbon::parse($dateOfBirth)->age; echo "You are ". $age . " years old";-1
You are 29 years old
You can use the parse() method with age on the \Carbon\Carbon class to get the age on the basis of date of birth.
You can use this code snippet in your controller methods and you can also use it in your view file by passing the value to it.
Calculate age in Laravel with years, months and days using Carbon
//Date format Y-m-d $dateOfBirth = '1992-4-4'; $age = \Carbon\Carbon::parse($dateOfBirth)->diff(\Carbon\Carbon::now())->format('%y years, %m months and %d days'); echo $age;0
29 years, 1 months and 15 days
You can also calculate age from a given date in Laravel with years, months, and days using Carbon class. You can use this code snippet in the controller and in your view files.
Random Code Snippet Queries: Laravel
- How to disable timestamps in laravel
- How to remove package from laravel
- Split an Eloquent Collection by half in Laravel
- SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.post_id' in 'where clause
- Call to a member function pluck() on null
- How to authenticate admin users in Laravel ?
- How to get count of all records created at yesterday
- How to pass external link in laravel blade to anchor tag
- Get Array of IDs from Eloquent Collection
- Validation errors for multiple forms on same page Laravel
- How to get column names from table in Laravel
- How to check email is valid or not in Laravel
- How to display 1 day ago in comments in laravel view
- How to create laravel project using composer
- Laravel insert query not working
- How to create static page in Laravel
- Insert data with form validation using ajax in laravel
- Remove several global scope from query
- Rendering HTML from database table to view in Laravel
- Add [name] to fillable property to allow mass assignment on [App\Models\Project]
- Laravel 5.4 save data to database
- PhpMyAdmin - Error The mysqli extension is missing
- Laravel 9 pagination with search filter
- How to get list of all views file in laravel
- How to Get records between two dates in Laravel