Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Xanger's avatar
Level 1

Timezone issues in laravel application

I have a timezone issue when saving an item in created_at, it uses the UTC date, while I have set Europe/Rome.

To begin with, I did these debug steps:

  • SQL select now(); returns the correct date: 2024-06-01 17:44:18
  • PHP with Carbon returns the wrong date: 2024-06-01 15:46:43
        $now = Carbon::now();
        echo $now; 

Using:

$currentTimezone = date_default_timezone_get();
echo $currentTimezone;

It prints "UTC".

The server's datetime is correct. If I create a PHP file (without using Laravel) with

$currentTimezone = date_default_timezone_get();
echo $currentTimezone;

The timezone is correct...

In Laravel, I have:

app.php
'timezone' => env('APP_TIMEZONE', 'Europe/Rome'),

.env
APP_TIMEZONE='Europe/Rome'

After making the change, I use php artisan config:clear. How can I fix this timezone issue?

0 likes
5 replies
jlrdw's avatar

Did you clear your config cache?

artisan config:clear

Edit:

Most store UTC and display local time for the user. You have to make a column to store the users timezone.

Xanger's avatar
Level 1

This happens when I save an admin content, from filamentphp, I've already used config:clear, but it keeps not taking the default timezone....

I want him to take the default timezone: Europe/Rome for created_at, I don't want to use carbon to convert all dates and times...

jlrdw's avatar

@Xanger there is this: https://filamentphp.com/docs/3.x/forms/fields/date-time-picker#timezones

Are you sure you changed the timezone here:

    /*
    |--------------------------------------------------------------------------
    | Application Timezone
    |--------------------------------------------------------------------------
    |
    | Here you may specify the default timezone for your application, which
    | will be used by the PHP date and date-time functions. The timezone
    | is set to "UTC" by default as it is suitable for most use cases.
    |
    */

    'timezone' => env('APP_TIMEZONE', 'UTC'),

In config\app.php ?

Xanger's avatar
Level 1

@jlrdw Hi! Yes as you can read from my main message, I put as default key in app.php Europe/Rome, also I put it as well in the .env file.

The link you passed me from Filamentphp is just to tell the datatime which timezone to take... The timezone is wrong at the base of the Laravel project....

As already mentioned, if I put Carbon::now() or date_default_timezone_get() on a Laravel controller, the timezone is in UTC, if I create a php file inside public with date_default_timezone_get, the timezone is correct... So the problem is really on Laravel and not Filmanetphp

In the controller if I put:

$currentTimezone = date_default_timezone_get();
echo "The default timezone of the server is: $currentTimezone \n";
echo env('APP_TIMEZONE');

my output is:

The default timezone of the server is: UTC
Europe/Rome

So the env file is cached well.

LukeSavefrogs's avatar

I had the same exact problem and the issue was that I was taking advantage of mysql default value set to CURRENT_TIMESTAMP.

Laravel used the correct timezone, but when saving the model to the database without explicitly passing the datetime fields, mysql was using the UTC timezone.

Here I can see 3 possible solutions (from best to worst):

  1. Continue storing the datetime fields in UTC and only convert to user timezone in the presentation layer (in this case FilamentPHP using the *Field::configureUsing(...) method as explained here)
  2. Do not rely on database defaults and pass the datetime fields to every model creation
  3. Change the mysql timezone 💀
1 like

Please or to participate in this conversation.