How to set timezone in Yii2

PHP

I need to change the timezone in my Yii2 application to "Europe/Berlin". I've tried to configure it but it doesn't seem to work for me. How can i achieve this?

Running this in my PHP application returns “UTC”.

echo date_default_timezone_get()
Programming Software OpenSource Question created: 2020-08-18 08:06 Yukisami

8

Yii2 uses UTC as default timezone if the timezone is not configured in your php.ini or in your application configuration file. There are two ways to solve this issue:

1 Solution) You could modify you php.ini and set:

date.timezone=Europe/Berlin

If your are using php-fpm don't forget to restart the fpm service by running this command: “/etc/init.d/php-fpm restart”. In that way the changes in your php.ini file will be permitted. 

2 Solution) You could set the timezone directly in your Yii2 configuration file. This will also overwrite the timezone set in the php.ini.

$config = [ 
   'timeZone' => 'London/Berlin',
   ...
]
answered 2020-08-18 08:17 d3m