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 Create Candlestick Chart In Laravel 9 Using Highcharts
How To Create Candlestick Char...

In this article, we will see how to create a candlestick chart in laravel 9 using highcharts. A candlestick is a ty...

Read More

Oct-06-2022

Connecting Laravel Queues with Python for Task Processing
Connecting Laravel Queues with...

In this guide, I’ll show you how to connect Laravel queues with Python to handle resource-heavy tasks such as data...

Read More

Nov-12-2024

Laravel 9 Many To Many Relationship Example
Laravel 9 Many To Many Relatio...

In this article, we will see laravel 9 many to many relationship example. Also, you can use many to many relationsh...

Read More

Apr-03-2022

How To Get Element By Data Attribute In jQuery
How To Get Element By Data Att...

In this article, we'll learn how to locate elements using data-attribute values. We can do this using jQuery and CSS...

Read More

Jul-11-2022