How to Convert UTC Time to Local Time in Laravel

  Feb 2024
  ITSolutionsGuides
  Category: Laravel
How to Convert UTC Time to Local Time in Laravel

Welcome To ITSolutionsGuides,


To Convert UTC Time to Local Time in Laravel we will use setTimezone() we need to specify the local time zone in the setTimezone(). Are you struggling with converting UTC time to local time in Laravel? Look no further! With Laravel's powerful Carbon library, mastering this task is a breeze. With a few simple steps, you'll learn how to utilize Laravel's Carbon methods to convert UTC time to your desired local time zone, ensuring your application displays accurate timestamps for users worldwide. Lets see how to effortlessly convert UTC time to local time in Laravel using Carbon. Since using UTC time format for different regions of earth will create complication hence we should convert the UTC to the Local time zone

Let's Start Coding

By using the custom timestamp and the pass to Carbon::parse() from the Carbon class.

Example 1)

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

Let's Start Coding

By using the current timestamp and the pass to Carbon::now() from the Carbon class.


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

We hope it helps everyone. Thanks for supporting ITSolutionsGuides and keep supporting us also follow us in social media platforms.

Subscribe for NewsLetter

Be the first to know about releases and tutorial news and solutions.

We care about your data in our privacy policy.

ITSolutionsGuides

ITSolutionsGuides was started mainly to provide good and quality web solutions for all the developers. We provide tutorials to support all the developers and also we try to provide solutions to the errors we face while coding.

Contact US

ITSolutionsGuides, provide users with an easy-to-use form to reach out for support or inquiries.

whatsapp  gmail  instagram-new--v1  facebook-circled  twitter-circled  linkedin  github  pinterest 

Copyright © 2023 - 2024 All rights reserved | ITSolutionsGuides