How to Change Date Format in Laravel 11?

26-Mar-2024

.

Admin

How to Change Date Format in Laravel 11?

Hello Dev,

Today we'll explore how to alter date formats using Carbon within a Laravel 11 application. Occasionally, you may need to adjust the date format within your Laravel 11 app. In such cases, Carbon comes to the rescue, offering numerous methods for manipulating dates effortlessly. Below, I'll provide straightforward examples demonstrating how to convert date formats in Laravel 11 using Carbon.

What is Carbon in Laravel?

Carbon serves as a versatile date and time utility within Laravel, offering developers a plethora of convenient methods for managing dates, times, and time zones. By extending PHP's native DateTime class, Carbon enhances the capabilities for handling temporal data in Laravel applications. Its features include parsing, formatting, manipulating, and comparing dates with ease, thereby boosting the efficiency and clarity of code that deals with temporal information.

1) Laravel 11 Change Date Format with Model


2) Laravel 11 Change Date Format Y-m-d H:i:s to d-m-Y

3) Laravel 11 Change Date Format Y-m-d to m/d/Y

4) Laravel 11 Change Date Format m/d/Y to Y-m-d

5) Laravel 11 Change Date Format Y-m-d to d/m/Y

I will show you controller code with output:

1) Laravel 11 Change Date Format with Model:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\User;

class DemoController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$user = User::first();

$newDate = $user->created_at>format('d-m-Y');

dd($newDate);

}

}

Output

11-02-2024

2) Laravel 11 Change Date Format Y-m-d H:i:s to d-m-Y:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Illuminate\Support\Carbon;

class DemoController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$date = date('Y-m-d H:i:s');

$newDate = Carbon::createFromFormat('Y-m-d H:i:s', $date)

->format('m/d/Y');

dd($newDate);

}

}

Output

03/24/2024

3) Laravel 11 Change Date Format Y-m-d to m/d/Y:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Illuminate\Support\Carbon;

class DemoController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$date = "2024-03-24";

$newDate = Carbon::createFromFormat('Y-m-d', $date)

->format('m/d/Y');

dd($newDate);

}

}

Output

03/24/2024

4) Laravel 11 Change Date Format m/d/Y to Y-m-d:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Illuminate\Support\Carbon;

class DemoController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$date = "03/24/2024";

$newDate = Carbon::createFromFormat('m/d/Y', $date)

->format('Y-m-d');

dd($newDate);

}

}

Output

2024-03-24

5) Laravel 11 Change Date Format Y-m-d to d/m/Y:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Illuminate\Support\Carbon;

class DemoController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$date = "2024-03-24";

$newDate = Carbon::createFromFormat('Y-m-d', $date)

->format('d/m/Y');

dd($newDate);

}

}

Output

24/03/2024

I hope it can help you...

#Laravel 11