How to Convert UTC Time to Local Time in Laravel 11

Websolutionstuff | Jul-03-2024 | Categories : Laravel PHP

Hello, laravel web developers! In this article, we'll see how to convert UTC time to local time in laravel 11. Here, we'll use the carbon parse function to convert local time in laravel 11. Use Carbon's setTimezone() method in Laravel 11 to convert any UTC time to your local setting.

Sometimes, we need to convert time to local time zones for our projects. So, we'll convert UTC time zone to specific local time zones.

Laravel 11 Convert UTC time to local time

laravel 11 convert utc time to local time

 

Example:

<?php
     
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
use Carbon\Carbon;
    
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $time = Carbon::parse('2024-06-29 21:20:50')
                        ->setTimezone('Asia/Kolkata')
                        ->toDateTimeString();
                          
        dd($time);
    }
}

Output:

2024-06-30 02:50:50

 

Example:

<?php
     
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
use Carbon\Carbon;
    
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $time = Carbon::now()
                    ->setTimezone('Asia/Kolkata')
                    ->toDateTimeString();
  
        dd($time);
    }
}

Output:

2024-06-29 19:20:10

 


You might also like:

Recommended Post
Featured Post
How to Use Bitmasks for Efficient Data Filtering?
How to Use Bitmasks for Effici...

Data filtering might not sound like the most thrilling topic, but when it comes to processing large volumes of informati...

Read More

Oct-25-2023

Laravel 8 Has Many Through Relationship Example
Laravel 8 Has Many Through Rel...

In this example we will see laravel 8 has many through relationship example. hasManyThrough relationship difficult to un...

Read More

Nov-17-2021

Drag and Drop File Upload Using Dropzone js in Laravel 8
Drag and Drop File Upload Usin...

This article will show drag and drop file upload using dropzone js in laravel 8. DropzoneJS is an open-source library th...

Read More

Oct-20-2020

How To Add Tooltip In React JS
How To Add Tooltip In React JS

In this article, we will see how to add a tooltip in react js. Tooltips display informative text when users hover o...

Read More

Sep-12-2022