How to Convert UTC Time to Local Time in Laravel

How to Convert UTC Time to Local Time in Laravel

To convert UTC time to local time in Laravel, you can use the Carbon library. The Carbon library provides a number of methods for working with dates and times, including the timezone() method.

To convert a UTC time to local time, you can use the following code:

The timezone() method takes the name of a timezone as its argument. You can find a list of all supported timezones in the PHP documentation.

You can also use the timezone() method to set the default timezone for your Laravel application. To do this, add the following code to your config/app.php file:

<?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);
    }
}

This will output the following HTML:

2023-10-19 01:55:48

You can use the Carbon library to convert UTC time to local time in any Laravel view, using the same methods as described above.

For more like this click here.

Leave a Reply