Laravel 10 - Change Date Format Using Jenssegers/Date Package
Teams applying “Laravel 10 - Change Date Format Using Jenssegers/Date Package” in a production project can use the full explanation to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.
Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.
Hi Friends,
Today our leading topic is Laravel 10 change date format using jenssegers/date package. you will learn date multiple languages. I explained simply how to create in multiple languages Laravel 10. This tutorial will give you a simple example of changing the date format. Alright, let’s dive into the steps.
In this article, I will let you know how to change the date format using jenssegers/date composer package in Laravel 10. jenssegers/date package provides us with a Date facade. Date facade through we can simply change the date format, and calculate the difference between two dates.
jenssegers/date package provides an amazing feature in multi-language support. If you want to display your data in a specific language then they support almost languages as listed here: Albanian, Arabic, Azerbaijani, Bangla, Basque, Brazilian Portuguese, Bulgarian, Catalan, Croatian, etc.
You can simply use this plugin and it's amazing. So you have to just follow the below example.
First of all, you need required jenssegers/date package installed.
Download Laravel
Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip the following step.
composer create-project laravel/laravel example-app
Install Package now we have to install jenssegers/date composer package for the date facade. So simply run below composer command for the install package.
composer require jenssegers/date
After install successfully above jenssegers/date package we need to add to the providers and aliases array of configuration files. So let's add the following way:
config/app.php
<?php
return [
'providers' => [
Jenssegers\Date\DateServiceProvider::class,
],
'aliases' => [
'Date' => Jenssegers\Date\Date::class,
]
Add Route
Simple Example of Language Support:
routes/web.php
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('date-formate', function () {
\Date::setLocale('hi');
$date = Date::now()->format('l j F Y H:i:s');
dd($date);
});
Change Date Format:
routes/web.php
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('date-formate', function () {
\Date::setLocale('nl');
$date = Date::parse('2021-12-12 01:01:10')->format('l j F Y');
dd($date);
});
Run Laravel App:
All steps have been done, now you have to type the given command and hit enter to run the Laravel app:
php artisan serve
Now, you have to open the web browser, type the given URL and view the app output:
http://localhost:8000/date-formate
Output :
zo. 12 december 2021
It will help you...
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial