laravel 有什么办法可以在laravel 4 database.php 配置文件中设置时区?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17493472/
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
Is there any way I can set timezone in laravel 4 database.php configuration file?
提问by Mohammad Shoriful Islam Ronju
Is there any way I can set timezone in laravel 4 database.php configuration file (like 'timezone' => 'UTC'
)? I'm using mysql date, now and other functions which is giving me two separate time for production and development.
有什么办法可以在 laravel 4 database.php 配置文件(如'timezone' => 'UTC'
)中设置时区?我正在使用 mysql date、now 和其他函数,这给了我两个单独的生产和开发时间。
Thanks
谢谢
回答by Chris J
Go to app->config->app.php in your laravel directory. On line 43 you can alter the default timezone to whatever you need.
转到 Laravel 目录中的 app->config->app.php。在第 43 行,您可以将默认时区更改为您需要的任何内容。
Laravel 4 default:
Laravel 4 默认:
'timezone' => 'UTC',
Your timezone:
您的时区:
'timezone' => 'America/Los_Angeles',
This change will then carry over to your database migrations. Also, if you need to set different timezones based on environment settings, simply add a directory in app->config directory that corresponds to the environment name you specified (ie - "production"). Place a duplicate of "app.php" into your newly created directory and alter the timezone setting on line 43 accordingly.
然后,此更改将延续到您的数据库迁移。此外,如果您需要根据环境设置设置不同的时区,只需在 app->config 目录中添加一个与您指定的环境名称相对应的目录(即 - “生产”)。将“app.php”的副本放入新创建的目录中,并相应地更改第 43 行的时区设置。
回答by user2094178
Yes, you can add the following at the top of database.php, before return array:
是的,您可以在database.php的顶部,在返回数组之前添加以下内容:
date_default_timezone_set('UTC');
If you want to New York:
如果你想去纽约:
date_default_timezone_set('America/New_york');
And so on. I have tested it and works fine to me.
等等。我已经测试过了,对我来说效果很好。
回答by instead
None of currently available answers helped me, so I came up with that solution:
当前可用的答案都没有帮助我,所以我想出了这个解决方案:
'mysql' => array(
...
'options' => [
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET time_zone = "+00:00"',
],
),
'options'
should be added to mysql
connection data (app/config/database.php).
'options'
应该添加到mysql
连接数据 (app/config/database.php)。
回答by shanthan
Add 'timezone' => '+05:30' within 'mysql' in your config->database.php file
在 config->database.php 文件中的 'mysql' 中添加 'timezone' => '+05:30'
'mysql' => [
'driver' => 'mysql',
.....
.....
.....
'timezone' => '+05:30'
],