Lumen timezone 時區(qū)設置方法(慢了8個小時)
根據(jù) Laravel 4.x 和 5.0 的經驗, 只需要到 config/app.php 中設置下 ‘timezone' 參數(shù)為 ‘PRC' 就好了, 找到 Lumen 的 config 目錄, 在 /vendor/laravel/lumen-framework/config 路徑下, 但是 config/app.php 的參數(shù)選項中沒有 timezone 參數(shù)選項, 手動加上后也是無效的。
然后想到 Laravel 5 的 .env 文件, 結果發(fā)現(xiàn) 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, ],
在這里有個數(shù)據(jù)庫的 timezone 設置, 默認 +00:00, 也就是 UTC 時間, 改成 +08:00 問題解決。由于項目啟用了 .env 配置文件, 所以最終是在 .env 文件里添加了一行
DB_TIMEZONE=+08:00
數(shù)據(jù)庫 timezone 問題解決。
數(shù)據(jù)庫的 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 參數(shù)為 APP_TIMEZONE, 值為 UTC, 在這里將 UTC 改為 PRC, 或者在 .env 文件里添加
APP_TIMEZONE=PRC
lumen php 的時區(qū)設置問題解決。
Lumen 時區(qū)設置總結
編輯 .env 文件添加配置
APP_TIMEZONE=PRC DB_TIMEZONE=+08:00
若沒啟用 .env 配置文件, 編輯
/vendor/laravel/lumen-framework/config/database.php /vendor/laravel/lumen-framework/src/Application.php
分別修改 APP_TIMEZONE 和 DB_TIMEZONE 參數(shù)值。
啟用 .env 配置文件
將 Lumen 根目錄下的 .env.example 文件重命名為 .env, 編輯 /bootstrap/app.php, 取消如下行代碼的注釋
Dotenv::load(__DIR__.'/../');
補充:
因為lumen默認使用格林尼治時間,需要轉成北京時間。
在.env中加入
APP_TIMEZONE=PRC
DB_TIMEZONE=+08:00
這樣時間就正確了
相關文章
php獲取mysql數(shù)據(jù)庫中的所有表名的代碼
如何用PHP獲取MYSQL數(shù)據(jù)庫的所有表名?記得在mysql命令行下面有條命令SHOW TABLES是顯示mysql數(shù)據(jù)庫里面所有數(shù)據(jù)表的,那么就用這條命令來遍歷數(shù)據(jù)表名吧2011-04-04libmysql.dll與php.ini是否真的要拷貝到c:\windows目錄下呢
很多安裝PHP的教程,都是教大家把php里的libmysql.dll拷貝到c:\windows目錄下(有的教程會說還要把php.ini等文件拷到系統(tǒng)目錄的,其實一個文件都不用拷貝去的。)。2010-03-03PHP實現(xiàn)簡單ajax Loading加載功能示例
這篇文章主要介紹了PHP實現(xiàn)簡單ajax Loading加載功能的方法,結合實例形式分析了ajax加載的原理、操作技巧與相關注意事項,需要的朋友可以參考下2016-12-12