How to Change Date Format in Laravel 11

Websolutionstuff | Apr-29-2024 | Categories : Laravel PHP

Hello developers! In this article, we'll see how to change the date format in laravel 11. Here, we'll learn in laravel 11 to change the date format with the help of carbon. Carbon is a PHP library for working with dates and times. It provides an easy-to-use and intuitive API for manipulating dates and times.

Carbon can support multiple date format options such as d-m-Y, Y-m-d, and m/d/Y, and you can easily parse, format, manipulate, and compare dates.

Carbon Date Format in Laravel 11

 

Change Date Format with Model

Now, we'll convert created_at date to d-m-Y format.

<?php

namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\User;
  
class UserController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $user = User::first();
        $created_at = $user->created_at->format('d-m-Y');
        
        dd($created_at);
    }
}

Output:

27-04-2024

 

Change Date Format Y-m-d H:i:s to d-m-Y

Then, we'll convert the date format Y-md H:i:s to d-m-Y using the carbon function createFromFormat.

<?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');

        $MDYDate = Carbon::createFromFormat('Y-m-d H:i:s', $date)->format('m/d/Y');

        dd($MDYDate);
    }
}

Output:

04/27/2024

 

Change Date Format Y-m-d to m/d/Y

Then, we'll change format to m/d/Y using carbon.

<?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-04-27";

        $MDYDate = Carbon::createFromFormat('Y-m-d', $date)->format('m/d/Y');
  
        dd($MDYDate);
    }
}

Output:

04/27/2024

 

Change Date Format m/d/Y to Y-m-d

Next, we'll convert the date format to Y-m-d format

<?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 = "04/27/2024";

        $YMDDate = Carbon::createFromFormat('m/d/Y', $date)->format('Y-m-d');
  
        dd($YMDDate);
    }
}

Output:

2024-04-27

 

Month Sort Name

Now, we'll convert the date with the month sort name.

$date = "2024-04-24";
	
$newDateFormat = \Carbon\Carbon::createFromFormat('Y-m-d', $date)->format('d M Y');

Output:

24 APR 2024

 

Month Full Name

Next, we'll convert the date with the month's full name using the carbon.

$date = "2024-04-24";
	
$newDateFormat = \Carbon\Carbon::createFromFormat('Y-m-d', $date)->format('d F Y');

Output:

15 April 2024

 


You might also like:

Recommended Post
Featured Post
Laravel 11 Socialite Login with Facebook Account
Laravel 11 Socialite Login wit...

Hello, laravel web developers! In this article, we'll see how to login with Facebook in laravel 11 Socialite. In lar...

Read More

Aug-07-2024

Laravel 8 User Role and Permission
Laravel 8 User Role and Permis...

In this post i will show you laravel 8 user role and permission with example, here we will see how to set user role and...

Read More

Jun-02-2021

How To Get Current Date And Time In React JS
How To Get Current Date And Ti...

In this article, we will see how to get the current date and time in react js. You can get the current date and tim...

Read More

Sep-02-2022

How To Add Datepicker In Angular 15 Material
How To Add Datepicker In Angul...

In this tutorial, I will guide you through the process of adding a datepicker component to your Angular 15 application u...

Read More

Jun-28-2023