How to Convert UTC Time to Local Time in Laravel?

By Hardik Savani November 5, 2023 Category : Laravel

Hello Friends,

Here, I will show you how to work laravel convert utc to local time. we will help you to give an example of laravel carbon convert utc to local time. I explained simply about how to convert utc time to local time in laravel. I’m going to show you about how to convert utc to local time in laravel. So, let's follow a few steps to create an example of convert utc to local time laravel.

we will use Carbon setTimezone() method to convert utc time to local time in laravel. you need to add your local timezone as argument in setTimezone() method. in this example, we will convert utc time to my local time "Asia/Kolkata".

So, let's see the simple examples with code:

Example 1:

you can see the simple example with custom date and time:

<?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('2023-10-18 20:25:48')

->setTimezone('Asia/Kolkata')

->toDateTimeString();

dd($time);

}

}

Output:

2023-10-19 01:55:48

Example 2:

you can see the simple example with now date and time:

<?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:

2023-10-19 09:23:56

I hope it can help you...

Tags :
Shares