How to Convert UTC Time to Local Time in Laravel 10

Websolutionstuff | Mar-11-2024 | Categories : Laravel PHP

As a Laravel developer, working with time zones is a common requirement, especially when dealing with global applications. One crucial aspect is converting UTC (Coordinated Universal Time) to local time, ensuring that users across different regions see timestamps in their respective time zones.

In this guide, I'll walk you through the steps to effectively handle UTC to local time conversions in Laravel 10.

So, let's see how to convert UTC to local time in laravel 10, laravel 10 converts UTC to local time, php converts UTC to local time, and UTC to local in laravel 8/9/10.

Step 1: Configure Laravel's Timezone

In Laravel, configuring the application's default timezone is the initial step. Open the config/app.php file and locate the timezone key. Set it to your preferred time zone. For instance, if you want to set it to 'UTC', you would write:

'timezone' => 'UTC',

 

Step 2: Create a Helper Function

To streamline the process of converting UTC to local time throughout your application, creating a helper function is beneficial. Open the app/Helpers directory or create it if it doesn't exist. Then, create a new file, for example, TimeHelper.php, and define your helper function:

<?php

// app/Helpers/TimeHelper.php

use Carbon\Carbon;

function convertToLocale($time)
{
    return Carbon::parse($time)->timezone(config('app.timezone'));
}

 

Step 3: Implement the Helper Function

Utilize the convertToLocale helper function in your controllers, views, or any other relevant components where UTC needs to be converted to local time.

use App\Helpers\TimeHelper;

public function show($id)
{
    $post = Post::find($id);
    $localizedCreatedAt = TimeHelper::convertToLocale($post->created_at);

    return view('post.show', compact('post', 'localizedCreatedAt'));
}

 

Step 4: Display Localized Time in Views

Now that you've converted the UTC to local time, you can easily display it in your views. For example:

<p>Created at: {{ $localizedCreatedAt->format('Y-m-d H:i:s') }}</p>

This will output the localized creation time of the post.

Example:

<?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-03-01 20:33:10

 

Conclusion

Converting UTC to local time in Laravel 8, Laravel 9, and Laravel 10 is essential for providing a seamless user experience across various time zones. By following these steps and leveraging Laravel's powerful features like Carbon, you can effortlessly manage time zone conversions in your application.

 


You might also like:

Recommended Post
Featured Post
How To Encrypt And Decrypt String In Laravel 8
How To Encrypt And Decrypt Str...

In this example we will see how to encrypt and decrypt string in laravel 8 using crypt helper, As we all know laravel fr...

Read More

May-07-2021

How to Convert PDF to Image in Laravel 10
How to Convert PDF to Image in...

Greetings, Laravel enthusiasts! Today, let's unravel a common challenge in web development – converting PDFs t...

Read More

Dec-22-2023

Laravel 8 Yajra Datatable Example Tutorial
Laravel 8 Yajra Datatable Exam...

In this article, we will see the laravel 8 yajra datatable example tutorial. Datatable provides users to many...

Read More

Sep-30-2020

Laravel 9 whereColumn Query Example
Laravel 9 whereColumn Query Ex...

In this article, we will see laravel 9 whereColumn() query example. The whereCoulmn method is used to verify whethe...

Read More

Oct-21-2022