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

Rretzko's avatar
Level 15

Time defaults to midnight UTC

Hi All - I can't see what I'm doing wrong here, but I'm sure it's me! The problem is: I have a DATETIME field (version_date) that is recording a date/time string properly

id vid	date_type        version_date		created_at			updated_at
8	1	student_close	2024-07-23 17:00:00	2024-07-22 19:45:22	2024-07-22 19:46:07

Using tinker, when pull this model, I get the right result:

> $vcd = \App\Models\Events\Versions\VersionConfigDate::find(8)
= App\Models\Events\Versions\VersionConfigDate {#6848
    id: 8,
    version_id: 1,
    date_type: "student_close",
    version_date: "2024-07-23 17:00:00",
    created_at: "2024-07-22 19:45:22",
    updated_at: "2024-07-22 19:46:07",
  }

But when I isolate the field in tinker or in my app, I get this:

> $vcd->version_date
= Illuminate\Support\Carbon @1721692800 {#6165
    date: 2024-07-23 00:00:00.0 UTC (+00:00),
  }

with the correct date but a UTC midnight time.

I have the timezone in my .env set as:

APP_TIMEZONE=America/New_York

and this is referenced in my app.php

'timezone' => env('APP_TIMEZONE', 'America/New_York'),

When I pull the 'create_at' value, that works as expected:

> $vcd->created_at
= Illuminate\Support\Carbon @1721677522 {#7119
    date: 2024-07-22 19:45:22.0 UTC (+00:00),
  }

Any and all help is appreciated!

0 likes
5 replies
LaryAI's avatar
Level 58

It looks like the issue you're encountering is related to how Laravel is handling the version_date field. By default, Laravel treats created_at and updated_at fields as Carbon instances and applies the timezone settings correctly. However, for custom datetime fields like version_date, you need to explicitly tell Laravel to treat them as Carbon instances and apply the timezone settings.

Here's how you can solve this issue:

  1. Ensure the version_date field is cast to a datetime in your model:

    In your VersionConfigDate model, you should add the version_date field to the $casts property to ensure it is treated as a datetime.

    class VersionConfigDate extends Model
    {
        protected $casts = [
            'version_date' => 'datetime',
        ];
    }
    
  2. Set the timezone in your AppServiceProvider:

    You can set the default timezone for Carbon instances in the boot method of your AppServiceProvider.

    use Illuminate\Support\Carbon;
    
    class AppServiceProvider extends ServiceProvider
    {
        public function boot()
        {
            // Set the default timezone for Carbon
            Carbon::setLocale(config('app.timezone'));
        }
    }
    
  3. Check your database connection configuration:

    Ensure that your database connection configuration in config/database.php is set to use the correct timezone.

    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
        'timezone' => env('DB_TIMEZONE', '+00:00'),
    ],
    
  4. Verify the timezone in your .env file:

    Ensure that the timezone is correctly set in your .env file.

    APP_TIMEZONE=America/New_York
    DB_TIMEZONE=+00:00
    

By following these steps, you should be able to ensure that the version_date field is treated correctly with the appropriate timezone settings. This should resolve the issue of the time defaulting to midnight UTC.

Rretzko's avatar
Level 15

I implemented the suggestions above and changed the value to 3:30pm but getting the same results:

PS C:\xampp\htdocs\staging\tdr2025> php artisan tinker
Psy Shell v0.12.4 (PHP 8.2.12 — cli) by Justin Hileman
> $vcd = \App\Models\Events\Versions\VersionConfigDate::find(8)
= App\Models\Events\Versions\VersionConfigDate {#6848
    id: 8,
    version_id: 1,
    date_type: "student_close",
    version_date: "2024-07-23 15:30:00",
    created_at: "2024-07-22 19:45:22",
    updated_at: "2024-07-22 20:28:10",
  }

> $vcd->version_date
= Illuminate\Support\Carbon @1721692800 {#6165
    date: 2024-07-23 00:00:00.0 UTC (+00:00),
  }
tykus's avatar

Do you also have an Accessor on the model for your version_date property?

Snapey's avatar
Snapey
Best Answer
Level 122

or a casts[] entry?

Rretzko's avatar
Level 15

Hi Guys - Thanks for the follow-up: I went back to grab a copy of the model for you and realized that I missed the casts() function in the model, which pointed 'version_date' => 'date'. I removed the

protected $casts = ['version_date' => 'datetime'];

suggested by LarryAI, changed the casts() function to

'version_date' => 'datetime' 

and everything's working as expected now. Thanks for helping me find my error on this!

Please or to participate in this conversation.