Convert UTC Time to Local Time

Posted by

I will demonstrate how to use Laravel to convert UTC to local time in this section. We’ll assist you by providing a sample of how to convert UTC to local time using Laravel Carbon. I gave a brief explanation on how to use Laravel to convert UTC time to local time. I will demonstrate how to use Laravel to convert UTC to local time. So let’s develop an example of converting UTC to local time in Ruby using a few simple steps.

The Carbon setTimezone() method will be utilized in Laravel to convert UTC time to local time. The setTimezone() function requires you to provide in your local timezone as an input. We shall convert the UTC time in this example to my local time, “Asia/Kolkata”.

Example 1:

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

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

0 0 votes
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback

[…] Convert UTC Time to Local Time […]

1
0
Would love your thoughts, please comment.x
()
x