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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM