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 Force Redirect Http to Https in Laravel
How to Force Redirect Http to...

In this small artical we will see how to force redirect http to https in laravel, Here i will show you two method in&nbs...

Read More

Aug-11-2021

Laravel 9 Yajra Datatable Example
Laravel 9 Yajra Datatable Exam...

In this artical we will see laravel 9 yajra datatable example. As we all used datatable on our backend side project, Her...

Read More

Mar-10-2022

Executing Python Scripts in Laravel A Step-by-Step Guide
Executing Python Scripts in La...

When working on a Laravel application, you may encounter scenarios where Python's robust libraries for data transfor...

Read More

Nov-26-2024

How To Integrate Swagger In laravel 10
How To Integrate Swagger In la...

In the dynamic realm of modern web development, ensuring that APIs are well-documented, easy to comprehend, and seamless...

Read More

Aug-18-2023