我有:
$criteria->select = "FROM_UNIXTIME(created, '%M, %Y')";
它可以正常工作并在mysql时区中返回时间。仅在此查询中需要set_timezone。因此,我无法更改时区MySQL服务器和App的时间。
在MySql中,我可以在查询set time_zone = "+01:00"
之前添加,但是如何在CdbCriteria中添加它。
在数据库连接中设置在afterOpen上设置[]。
尝试此
$config['components']['db'] = [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=localhost;dbname=my_dbname', 'username' => 'root', 'password' => 'root', 'charset' => 'utf8', 'tablePrefix' => 'acc_', 'on afterOpen' => function($event) { // set 'Asia/Bangkok' timezone $event->sender->createCommand("SET time_zone='+07:00';")->execute(); }, ];
参考link