swoft-cloud / swoft

🚀 PHP Microservice Full Coroutine Framework
https://swoft.org
Apache License 2.0
5.58k stars 786 forks source link

swoft 2 时区设置问题 #1423

Open jiancixiuxian opened 3 years ago

jiancixiuxian commented 3 years ago

swoft 2.0.9 在 .env 里面设置了 TIME_ZONE=Asia/Bangkok 在 bin/swoft 里面设置了 ini_set('date.timezone', 'Asia/Bangkok'); 在 php.ini 里也设置了时区 但都均为生效 请问下 我是少了 哪一步吗

jiancixiuxian commented 3 years ago

以解决 在 app/Application.php 处理

XuanYi2018 commented 3 years ago

你好,在swoft项目根目录的 app\Application.php 的 beforeInit 函数中设置即可,如下所示:

<?php declare(strict_types=1);
/**
 * This file is part of Swoft.
 *
 * @link     https://swoft.org
 * @document https://swoft.org/docs
 * @contact  group@swoft.org
 * @license  https://github.com/swoft-cloud/swoft/blob/master/LICENSE
 */

namespace App;

use Swoft\SwoftApplication;
use function date_default_timezone_set;

/**
 * Class Application
 *
 * @since 2.0
 */
class Application extends SwoftApplication
{
    protected function beforeInit(): void
    {
        parent::beforeInit();

        // you can init php setting.
        // 设置时区
        date_default_timezone_set('Asia/Shanghai');
    }