根據(jù) Laravel 4.x 和 5.0 的經(jīng)驗(yàn), 只需要到 config/app.php 中設(shè)置下 ‘timezone' 參數(shù)為 ‘PRC' 就好了, 找到 Lumen 的 config 目錄, 在 /vendor/laravel/lumen-framework/config 路徑下, 但是 config/app.php 的參數(shù)選項(xiàng)中沒(méi)有 timezone 參數(shù)選項(xiàng), 手動(dòng)加上后也是無(wú)效的。
然后想到 Laravel 5 的 .env 文件, 結(jié)果發(fā)現(xiàn) Lumen 的 .env 文件里也沒(méi)有關(guān)于 timezone 設(shè)置的選項(xiàng)。
又回到 config 目錄, 看看 config/database.php 中的設(shè)置, 關(guān)于 mysql 的默認(rèn)配置如下:
'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, ],
在這里有個(gè)數(shù)據(jù)庫(kù)的 timezone 設(shè)置, 默認(rèn) +00:00, 也就是 UTC 時(shí)間, 改成 +08:00 問(wèn)題解決。由于項(xiàng)目啟用了 .env 配置文件, 所以最終是在 .env 文件里添加了一行
DB_TIMEZONE=+08:00
數(shù)據(jù)庫(kù) timezone 問(wèn)題解決。
數(shù)據(jù)庫(kù)的 timezone 問(wèn)題雖然解決了, 但是 app 的 timezone 問(wèn)題還沒(méi)解決, 全局搜索 lumen 項(xiàng)目, 找用到 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 的時(shí)區(qū)設(shè)置問(wèn)題解決。
Lumen 時(shí)區(qū)設(shè)置總結(jié)
編輯 .env 文件添加配置
APP_TIMEZONE=PRC DB_TIMEZONE=+08:00
若沒(méi)啟用 .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__.'/../');
補(bǔ)充:
因?yàn)閘umen默認(rèn)使用格林尼治時(shí)間,需要轉(zhuǎn)成北京時(shí)間。
在.env中加入
APP_TIMEZONE=PRC
DB_TIMEZONE=+08:00
這樣時(shí)間就正確了
標(biāo)簽:太原 株洲 濱州 曲靖 興安盟 南昌 白酒營(yíng)銷(xiāo) 南京
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Lumen timezone 時(shí)區(qū)設(shè)置方法(慢了8個(gè)小時(shí))》,本文關(guān)鍵詞 Lumen,timezone,時(shí)區(qū),設(shè)置,方法,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。