UTC to Local Time Conversion in Laravel | websolutioncode.com
UTC to Local Time Conversion in Laravel | websolutioncode.com

UTC to Local Time Conversion in Laravel: A Step-by-Step Guide

Understanding Timezones in Laravel

Laravel utilizes PHP’s Carbon library to handle date and time. By default, Laravel uses the UTC to Local Time Conversion in Laravel, which is set in the config/app.php file as UTC. Users might be in different timezones, so converting UTC time to their local time becomes necessary.

Steps to Convert UTC Time to Local Time

Step 1: Configuration

Firstly, ensure your Laravel application’s timezone is correctly set in config/app.php:

'timezone' => 'UTC',

Step 2: Retrieve UTC Time from Database

Assuming you have a UTC timestamp stored in your database, fetch it using Eloquent:

$utcTime = YourModel::find($id)->created_at;

Step 3: Convert UTC to Local Time

Use the Carbon library to convert the UTC time to the user’s local time:

$localTime = $utcTime->setTimezone(auth()->user()->timezone);

Step 4: Display Local Time

Now, you can display the converted local time in your view:

{{ $localTime->format('Y-m-d H:i:s') }}

Replace 'Y-m-d H:i:s' with the desired format for your application.

Example Implementation

Let’s create a practical example to illustrate the process. Assume we have a Post model with a created_at field storing UTC time.

Migration File

Schema::create('posts', function (Blueprint $table) {
    $table->timestamps(); // Creates created_at and updated_at fields

Controller Logic

use App\Models\Post;

class PostController extends Controller
    public function show($id)
        $post = Post::find($id);
        $post->created_at; // UTC time

        $post->local_time = $post->created_at->setTimezone(auth()->user()->timezone);

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


<!-- post.show.blade.php -->

<h1>{{ $post->title }}</h1>
<p>Content: {{ $post->content }}</p>
<p>UTC Time: {{ $post->created_at->format('Y-m-d H:i:s') }}</p>
<p>Local Time: {{ $post->local_time->format('Y-m-d H:i:s') }}</p>

Handling timezones accurately is pivotal in web applications, and in Laravel, converting UTC time to local time is a common necessity. This guide simplifies the process of UTC to Local Time Conversion in Laravel, ensuring precise time displays for users in different regions.

Laravel adopts the UTC timezone as a default, set in the config/app.php file. This guide walks you through the necessary steps to seamlessly convert UTC timestamps stored in your database to a user’s local time.

By leveraging Laravel’s Carbon library, the process becomes straightforward. Retrieve the UTC timestamp from the database using Eloquent, and then use setTimezone() to convert it to the user’s specific timezone.

Implementing this conversion in your application involves setting up the correct configurations, fetching the UTC time, performing the conversion, and displaying the localized time in your views.

By following this step-by-step guide, you’ll empower your Laravel application to accurately present time-sensitive data to users worldwide. Bid farewell to timezone-related confusion and ensure an optimal user experience with precise UTC to Local Time Conversion in Laravel.


Correctly handling timezones is vital for providing a seamless user experience in Laravel applications. By following these steps and leveraging Laravel’s Carbon library, you can easily convert UTC time to a user’s local time, ensuring accurate and localized timestamp displays.

Check our tools website Word count
Check our tools website check More tutorial

Leave a Reply