laravel 如何在 lumen 5.2 中设置时区?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35273277/
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 13:11:59  来源:igfitidea点击:

How can I set Timezone in lumen 5.2?

phplaravellumenlumen-5.2

提问by rap-2-h

I did not find any relevant information (only tricks) about how to set the default timezone in Lumen 5.2. Is there any clean way to do this?

我没有找到任何有关如何在 Lumen 5.2 中设置默认时区的相关信息(只有技巧)。有没有干净的方法来做到这一点?

采纳答案by Derek Pollard

This is pretty easily done and shown in their documentation page:

这很容易完成并显示在他们的文档页面中

To set configuration values at runtime, pass an array to the config helper:

要在运行时设置配置值,请将数组传递给配置助手:

config(['app.timezone' => 'America/Chicago']);

Alternatively, in app/config.php:

或者,在app/config.php

'timezone' => 'UTC',

回答by edgji

In Lumen 5.2 the Application class actually reads from a APP_TIMEZONE environment variable.

在 Lumen 5.2 中,Application 类实际上从 APP_TIMEZONE 环境变量中读取。

You can easily set timezone via a .env file using or setting the environment variable on your server:

您可以使用或设置服务器上的环境变量通过 .env 文件轻松设置时区:

APP_TIMEZONE=UTC

回答by Torvic

None of the responses I've read in a lot of forums solves the problem, because in the file /vendor/laravel/lumen-framework/config/database.php there is this line:

我在很多论坛上读到的回复都没有解决问题,因为在文件 /vendor/laravel/lumen-framework/config/database.php 中有这一行:

'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'port'      => env('DB_PORT', 3306),
            'database'  => env('DB_DATABASE', 'forge'),
            'username'  => env('DB_USERNAME', 'forge'),
            'password'  => env('DB_PASSWORD', ''),
            'charset'   => env('DB_CHARSET', 'utf8'),
            'collation' => env('DB_COLLATION', 'utf8_unicode_ci'),
            'prefix'    => env('DB_PREFIX', ''),
            **'timezone'  => env('DB_TIMEZONE', '+00:00'),**
            'strict'    => env('DB_STRICT_MODE', false),
        ],

You need to rewrite this config file. Create a database.php file in config folder. Then copy all the content without the timezone line. This works for me.

您需要重写此配置文件。在 config 文件夹中创建一个 database.php 文件。然后复制没有时区行的所有内容。这对我有用。

回答by Rodrigo Pauletti

You can add your timezone in your .envfile

您可以在.env文件中添加时区

APP_TIMEZONE=YOUR_TIME_ZONE

Docs:

文档:

List of Timezone

时区列表

Lumen Documentation

流明文档

回答by Igor Parra

Just to resume and be super clear (at this year 2018):

只是为了恢复和超级清晰(在今年 2018 年):

All of the configuration options for the Lumen framework are stored in the .env file.

Lumen 框架的所有配置选项都存储在 .env 文件中。

In Lumen does not exista config/app.phpfile.

管腔不存在一个config/app.php文件。



But also, if we look at vendor/laravel/lumen-framework/src/Application.php

但是,如果我们看 vendor/laravel/lumen-framework/src/Application.php

/**
 * Create a new Lumen application instance.
 *
 * @param  string|null  $basePath
 * @return void
 */
public function __construct($basePath = null)
{
    ...
        date_default_timezone_set(env('APP_TIMEZONE', 'UTC'));
    ...

ref: https://github.com/laravel/lumen-framework/blob/5.6/src/Application.php#L83

参考:https: //github.com/laravel/lumen-framework/blob/5.6/src/Application.php#L83

We see that Lumen will not take any configvalue, just an envvalue to set the time zone.

我们看到 Lumen 不会取任何config值,只是一个env设置时区的值。

So the technique of copy/paste /laravel/lumen-framework/config directoryto use full "Laravel style" configuration filesin Lumen is not applicable in this case, and never was.

所以在 Lumen 中/laravel/lumen-framework/config directory使用的复制/粘贴技术full "Laravel style" configuration files在这种情况下不适用,而且从来都不是。

Besides: that technique is an old reference to the first version of Lumen.
ref: https://lumen.laravel.com/docs/5.1#configuration-files. (old docs)

此外:该技术是对 Lumen 第一版的旧参考。
参考:https: //lumen.laravel.com/docs/5.1#configuration-files。(旧文档)

In the current version 5.6 of Lumen that tip has been removed from the documentation and probably was a tip to help migrating from Laravel in the initial times of Lumen but is not longer a good practice. So use .env files always.
ref: https://lumen.laravel.com/docs/5.6#configuration(new docs)

在当前的 Lumen 5.6 版本中,该提示已从文档中删除,并且可能是在 Lumen 的初始阶段帮助从 Laravel 迁移的提示,但不再是一个好的做法。所以总是使用 .env 文件
参考:https: //lumen.laravel.com/docs/5.6#configuration(新文档)

回答by paranoid

In config/app.php
you can change it

config/app.php
你可以改变它

'timezone' => 'UTC',

'时区' => 'UTC',