• Lumen 时区设置


    根据 Laravel 4.x 和 5.0 的经验, 只需要到 config/app.php 中设置下 'timezone' 参数为 'PRC' 就好了, 找到 Lumen 的 config 目录, 在 /vendor/laravel/lumen-framework/config 路径下, 但是 config/app.php 的参数选项中没有 timezone 参数选项, 手动加上后也是无效的。

    然后想到 Laravel 5 的 .env 文件, 结果发现 Lumen 的 .env 文件里也没有关于 timezone 设置的选项。

    又回到 config 目录, 看看 config/database.php 中的设置, 关于 mysql 的默认配置如下:

    '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'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => env('DB_PREFIX', ''),
        'timezone'  => env('DB_TIMEZONE','+00:00'),
        'strict'    => false,
    ],

    在这里有个数据库的 timezone 设置, 默认 +00:00, 也就是 UTC 时间, 改成 +08:00 问题解决。由于项目启用了 .env 配置文件, 所以最终是在 .env 文件里添加了一行

    DB_TIMEZONE=+08:00

    数据库 timezone 问题解决。

    数据库的 timezone 问题虽然解决了, 但是 app 的 timezone 问题还没解决, 全局搜索 lumen 项目, 找用到 timezone 的地方, 在 /vendor/laravel/lumen-framework/src/Application.php 文件中找到了初始化 lumen timezone 部分的代码

    /**
     * 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'));
    
        $this->basePath = $basePath;
        $this->bootstrapContainer();
        $this->registerErrorHandling();
    }

    代码中使用的 .env 参数为 APP_TIMEZONE, 值为 UTC, 在这里将 UTC 改为 PRC, 或者在 .env 文件里添加

    APP_TIMEZONE=PRC

    lumen php 的时区设置问题解决。


    Lumen 时区设置总结

    编辑 .env 文件添加配置

    APP_TIMEZONE=PRC
    DB_TIMEZONE=+08:00

    若没启用 .env 配置文件, 编辑

    /vendor/laravel/lumen-framework/config/database.php
    /vendor/laravel/lumen-framework/src/Application.php

    分别修改 APP_TIMEZONEDB_TIMEZONE 参数值。

    启用 .env 配置文件

    将 Lumen 根目录下的 .env.example 文件重命名为 .env, 编辑 /bootstrap/app.php, 取消如下行代码的注释

    Dotenv::load(__DIR__.'/../');


    转自:http://www.widlabs.com/article/lumen-set-timezone
  • 相关阅读:
    平台升级至spring 4.3.0 运行稳定
    java过滤特殊字符的正则表达式
    xheditor-文件上传-java-支持html5-application/octet-stream
    java用正则方法验证文件名是否合法
    Java实现在线预览Word,Excel,Ppt文档
    为什么用freemarker视图?
    Java中判断String不为空的问题性能比较
    解决org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource
    Java Swing 使用非本地字体
    第三方包jintellitype实现Java设置全局热键
  • 原文地址:https://www.cnblogs.com/linguoguo/p/5586419.html
Copyright © 2020-2023  润新知