laravel Created_at 保存时区错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29366835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:15:55  来源:igfitidea点击:

Created_at save with the wrong timezone

laravel

提问by osos

I have a laravel app and i'm trying to save the users checkins and checkouts to my database

我有一个 Laravel 应用程序,我正在尝试将用户签入和签出保存到我​​的数据库中

i have a model Checkinsand i record it like created_at and updated_at

我有一个模型Checkins,我将它记录为 created_at 和 updated_at

on my localhost it save with the right time for my timezome ( Egypt ), i tried to changed app.phpfile to the following

在我的本地主机上,它为我的时区(埃及)保存了正确的时间,我尝试将app.php文件更改为以下内容

    |--------------------------------------------------------------------------
| Application Timezone
|--------------------------------------------------------------------------
|
| Here you may specify the default timezone for your application, which
| will be used by the PHP date and date-time functions. We have gone
| ahead and set this to a sensible default for you out of the box.
|
| This is a bad thing really i think there is a problem within the server, the datetime
*/

'timezone'        => 'EET',

it's working just fine on my localhost and save with the current right time, for the production it's save with UTC timezone

它在我的本地主机上运行良好并使用当前正确的时间保存,对于生产,它使用 UTC 时区保存

Also when i write in the production terminal the command date date, i get the following

此外,当我在生产终端中写入命令日期时date,我得到以下信息

Tue Mar 31 12:46:38 EET 2015

and i checked mysql for the timezone and i found it's getting the time from the system time

我检查了 mysql 的时区,我发现它从系统时间获取时间

SELECT @@global.time_zone, @@session.time_zone;
SYSTEM

What's wrong here ?

这里有什么问题?

UPDATE:

更新:

I created a php page with date('H:i:s');and it's print the right time

我创建了一个 php 页面,date('H:i:s');它在正确的时间打印

回答by Artur Grigio

To set the timezone for your Laravel app, change the 'timezone'in your config/app.php

要为您的 Laravel 应用设置时区,请更改'timezone'您的config/app.php

|--------------------------------------------------------------------------
| Application Timezone
|--------------------------------------------------------------------------
|
| Here you may specify the default timezone for your application, which
| will be used by the PHP date and date-time functions. We have gone
| ahead and set this to a sensible default for you out of the box.
|
| This is a bad thing really i think there is a problem within the server, the datetime
*/

'timezone'        => 'America/Los_Angeles',

You can find the timezone you need here.

您可以在此处找到您需要的时区。

回答by Md. Sahadat Hossain

As per php documentationIt is strongly recommended that you use the correct time zone for your location, such as Asia/Shanghaior Australia/PerthNot use EETor CET.

根据php 文档强烈建议您为您所在的位置使用正确的时区,例如Asia/ShanghaiAustralia/Perth不使用EETCET

Your can find timezone for your location here

您可以在此处找到您所在位置的时区

回答by chii

Maybe you use insertor insertGetIdto insert the data?

也许您使用insertinsertGetId插入数据?

If you use Eloquent(like ->save())to insert data, the time will be right.

如果你Eloquent(like ->save())用来插入数据,时间就对了。

回答by batsz

You have to change yout time zone in config/app.php

你必须改变你的时区 config/app.php

Also as is said hereyou must run following commands for your time zone changes to be saved:

也正如此处所说您必须运行以下命令才能保存时区更改:

php artisan cache:clear php artisan view:clear php artisan config:cache

php artisan cache:clear php artisan view:clear php artisan config:cache

回答by fduch

Looks like you forgot to run php artisan config:cache.

看起来您忘记运行 php artisan config:cache。