Convert UTC Time to Local Time in laravel

Posted by

In this part, I’ll walk through how to utilise Laravel to convert UTC to local time. We’ll help by giving you an example of how to use Laravel Carbon to convert UTC to local time. I briefly explained how to convert UTC time to local time using Laravel. I’ll walk through how to convert UTC to local time using Laravel. So let’s create a quick example in Ruby that shows how to convert UTC to local time in a few easy steps.

To convert UTC time to local time in Laravel, use the Carbon setTimezone() method. You must supply your local timezone as an input for the setTimezone() function. In this case, the UTC time will be converted to my local time.

Example of controller code:

<?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-02-18 20:25:48')
                        ->setTimezone('Asia/Kolkata')
                        ->toDateTimeString();
                          
        dd($time);
    }
}

OutPut:

2024-02-19 01:55:48

Example 2:

<?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-19 09:23:56

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x