Laravel 9 - Change Date Format Using Jenssegers/Date Package

10-Apr-2023

.

Admin

Laravel 9 - Change Date Format Using Jenssegers/Date Package

Hi Friends,

Today our leading topic is laravel 9 change date format using jenssegers/date package. you will learn date multiple language. I explained simply about how to create in multiple language laravel 9. This tutorial will give you simple example of change date formate. Alright, let’s dive into the steps.

In this article i will let you know how to change date format using jenssegers/date composer package in Laravel 9. jenssegers/date package provide us Date facade. Date facade through we can simply change date formate, calculate difference between two dates its.

jenssegers/date package provide amazing feature is multi-language support. If you want to display your date in specific language then they are 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 bellow example.

First of all you need required jenssegers/date package install.

Download Laravel


Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

composer create-project laravel/laravel example-app

Install Package

now we have to install jenssegers/date composer package for date facade. So simply run bellow composer command for install package.

composer require jenssegers/date

After install successfully above jenssegers/date package we need to add into providers and aliases array of configuration file. So let's add 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 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...

#Laravel 9