How to set Laravel Carbon timezone for timestamps?

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: A Comprehensive Guide on Setting Laravel Carbon Timezone for Timestamps Introduction Laravel is an extensible PHP framework that provides a powerful set of tools to handle various functionalities, including time-related tasks through its support for the Carbon library. In this blog post, we will cover how you can set global time zones for timestamps and ensure consistency in your Laravel project. Laravel's Timezone Support - Config/App.php Setting up a specific time zone in your Laravel application begins with adding it to the config/app.php file within your project. Open this file, locate the 'timezone' key under the 'aliases' array, and set its value to the desired standard time zone name (e.g., "Europe/London" for British Summer Time). Using Carbon Library with Global Timezone Carbon is a utility library that extends PHP's DateTime class, providing various methods for handling date and time manipulation. To set a global timezone for timestamps in Laravel, you can create a single instance of the Carbon\Carbon class using your application's configured time zone:
$carbon = new Carbon\Carbon();
From this point onwards, all instances created with Carbon::now() will use your defined global timezone. If you want to convert an existing timestamp stored in UTC to the application's configured time zone, follow these steps:
$carbonUtc = new \DateTime(); // Get current UTC time
$timeZone = new DateTimeZone('Europe/London'); // Your defined timezone
$convertedTimestamp = $carbonUtc->setTimezone($timeZone);
Making Timezone Conversion Automatic To make the time conversion automatic for all pivot timestamps in your Laravel project, you can utilize a custom mutator. This method allows you to modify the results of accessors and mutators from your model's attributes:
/**
* Define a mutator that converts all pivot timestamps from UTC to the configured timezone.
*/
\Illuminate\Database\Eloquent\Model::addGlobalScope('pivotTimestamp', function (\Illuminate\Database\Eloquent\Builder $builder) {
    $builder->withoutGlobalScopes(); // Remove all other scopes first
    $timeZone = new \DateTimeZone(config('app.timezone')); // Load the application's configured time zone
    // Apply the same timezone conversion as above for any timestamp stored in UTC
    foreach ($builder->getModel()->pivot as $key => $value) {
        if (is_array($value)) {
            foreach ($value as $subKey => $timestamp) {
                if (is_int($timestamp)) {
                    $value[$subKey] = new \DateTime(date('Y-m-d H:i:s', $timestamp), $timeZone);
                }
            }
        }
    }
});
In the above code, we traverse through any pivot timestamps in your model and convert them from UTC to your application's configured time zone. This ensures that all timestamps are consistent with your project's needs. Conclusion By following these guidelines, you can easily set a global timezone for timestamps within your Laravel project. Remember to incorporate best practices, such as using the Carbon library and configuring the correct time zone in your application settings. For more details on Laravel's Carbon library and date manipulation techniques, refer to the official documentation and resources at https://laravelcompany.com.