根据 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=PRCDB_TIMEZONE=+08:00
若没启用 .env 配置文件, 编辑
/vendor/laravel/lumen-framework/config/database.php/vendor/laravel/lumen-framework/src/Application.php
分别修改 APP_TIMEZONE 和 DB_TIMEZONE 参数值。
启用 .env 配置文件
将 Lumen 根目录下的 .env.example 文件重命名为 .env, 编辑 /bootstrap/app.php, 取消如下行代码的注释Dotenv::load(__DIR__."/../");
补充:
因为lumen默认使用格林尼治时间,需要转成北京时间。在.env中加入
APP_TIMEZONE=PRC DB_TIMEZONE=+08:00
这样时间就正确了