How to Convert UTC Time to Local Time in Laravel?
Introduction
Laravel uses UTC as its default timezone for storing timestamps. However, in real-world applications, we often need to display the time in the user’s local timezone. In this article, we’ll walk through how to convert UTC time to a local timezone using Laravel’s built-in tools and Carbon.
1. Set Timezone in Laravel
You can set your application's default timezone in config/app.php:
'timezone' => 'Asia/Kolkata', // or 'America/New_York', etc.
2. Convert UTC Time to Local Using Carbon
If you have a UTC timestamp (from database or external API), convert it like this:
use Carbon\Carbon;
$utcTime = '2025-06-09 12:00:00'; // UTC timestamp
$localTime = Carbon::createFromFormat('Y-m-d H:i:s', $utcTime, 'UTC')
->setTimezone(config('app.timezone'));
echo $localTime->format('Y-m-d h:i A'); // Output in local time
3. Convert Eloquent Timestamps Automatically
If your Eloquent models use timestamps, Laravel already converts them to the app’s timezone when accessed:
$user = User::find(1);
echo $user->created_at->format('Y-m-d h:i A');
This works if your app’s timezone is set properly in config/app.php.
4. Convert to User's Timezone (Optional)
If users have their own timezone setting:
$utcTime = $user->created_at;
$userTimezone = $user->timezone; // e.g. 'Europe/London'
echo $utcTime->setTimezone($userTimezone)->format('Y-m-d h:i A');
5. Storing vs Displaying
- Always store timestamps in UTC in your database.
- Always convert them to the user’s timezone when displaying.
Conclusion
Handling timezones correctly is critical in any application with global users. Laravel makes it easy with its Carbon integration and timezone configuration. By storing UTC and displaying local times, your app will stay accurate and user-friendly worldwide.
Comment / Reply From
You May Also Like
Popular Posts
-
laravel notes
- Post By anas
- June 21, 2025
Categories
Newsletter
Subscribe to our mailing list to get the new updates!