Laravel 10 - Change Date Format Using Jenssegers/Date Package

27-May-2023

.

Admin

Laravel 10 - Change Date Format Using Jenssegers/Date Package

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...

#Laravel 10