Skip to content

Eloquent's casted 'date' field disrespects Carbon instance's timezone when saving to database. #15122

@lezhnev74

Description

@lezhnev74

I faced a problem with saving date fields with Eloquent - it removed the timezone from Carbon instance and saved it in the timezone of the App.

So here my full note: http://lessthan12ms.com/save-all-dates-in-utc-for-multi-timezone-apps/

And to save time this is an example here:

<?php
dump(config('app.timezone'));
// OUTPUT: "UTC"

// We create a Carbon instance with timezone set to "GMT+3"
$date_in_local_timezone = Carbon::now('GMT+3');
// Let's  see the date we had initially
dump($date->toIso8601String())
// OUTPUT: "2015-01-01T12:00:00+0300"

// We then save it as "date" field
$model->some_date = $date_in_local_timezone;
$model->save();

// And then read it back from the model
dump($model->fresh()->some_date->toIso8601String());
// OUTPUT: "2015-01-01T12:00:00+0000"

As you can see we have the same datetime string but with timezone stripped.
I have my solution in the full note mentioned above.

Is it something to make a PR for or am I missing something?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions