To change the date format in Laravel, you can use the format()
method provided by the Carbon
library, which is included with Laravel by default. Here's an example of how you can change the date format.
1) Laravel 10 Change Date Format with Model
2) Laravel 10 Change Date Format Y-m-d H:i:s to d-m-Y
3) Laravel 10 Change Date Format Y-m-d to m/d/Y
4) Laravel 10 Change Date Format m/d/Y to Y-m-d
5) Laravel 10 Change Date Format Y-m-d to d/m/Y
Add Below Code in Controller
<?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()
{
//Method:1 Change Date Format d-m-Y:
$user = User::first();
$newDate = $user->created_at->format('d-m-Y');
dd($newDate);
// OR
//Method:2 Change Date Format Y-m-d H:i:s to d-m-Y:
$date = date('Y-m-d H:i:s');
$newDate = Carbon::createFromFormat('Y-m-d H:i:s', $date)
->format('m/d/Y');
dd($newDate);
// OR
//Method:3 Change Date Format Y-m-d to m/d/Y:
$date = "2023-02-22";
$newDate = Carbon::createFromFormat('Y-m-d', $date)
->format('m/d/Y');
dd($newDate);
//OR
//Method:4 Change Date Format m/d/Y to Y-m-d:
$date = "02/22/2023";
$newDate = Carbon::createFromFormat('m/d/Y', $date)
->format('Y-m-d');
dd($newDate);
//OR
//Method:5 Change Date Format Y-m-d to d/m/Y:
$date = "2023-02-22";
$newDate = Carbon::createFromFormat('Y-m-d', $date)
->format('d/m/Y');
dd($newDate);
}
}
I hope it can helps you, Thank You.