laravel / ideas

Issues board used for Laravel internals discussions.
938 stars 28 forks source link

[Proposal] Add a way to set date_format globally #904

Open shemgp opened 6 years ago

shemgp commented 6 years ago

I usually add a timezone in my timestamps (created_at, updated_at, deleted_at). This requires me to set protected $dateFormat per model or have a custom base model that has the required format. However, pivot->withTimestamps() won't follow those rules. Adding a global 'date_format' would fix the problem.

The implementation is a simple:


        return config('app.date_format', 'Y-m-d H:i:s');

in the framework/Illuminate/Database/Grammar.php.

Thank you for considering this.

rwinzhang commented 3 years ago

I would like to reopen and put this issue here https://github.com/laravel/framework/issues/35135

I happen to face this issue too. The $dateFormat on Model only affects when we save the model but not when we need to query. My use case is when I want to query with a specific timezone and Y-m-d H:i:s format makes the timezone information truncated and returns wrong results. I think we need a way to configure this like doing Grammar::setDateFormat('Y-m-d H:i:s.uP') on AppServiceProvider, so I can override it when needed. Please consider this suggestion @driesvints, Thanks.