Problem with timezone and hours

I’ve got this custom controller from where the others extend.




public function __construct($id, $module, $config = array()) {

    parent::__construct($id, $module, $config);

    Yii::$app->formatter->defaultTimeZone = 'Europe/Madrid';

    Yii::$app->setTimeZone('Europe/Madrid');

    Yii::$app->formatter->dateFormat = 'Y-MM-dd';

    Yii::$app->formatter->timeFormat = 'php:H:i:s';


}



Now when I show a date or I save it into the database it works fine. I save a date like the following:




$timezone = new \DateTimeZone('Europe/Madrid');

$date = new \DateTime(date(), $timezone);

$model->date = $date->format('Y-m-d H:i:s');



As I said, the value is saved correctly but when I show it it’s changed.




Yii::$app->formatter->asDate($model->date, 'php:d/m/Y H:i')



If I save 2015-04-28 10:00:00 it returns 28/04/2015 8:00. Looks like it takes away 2 hours.

If I remove the line


Yii::$app->setTimeZone('Europe/Madrid');

from the controller the app won’t work anymore because it fails everytime it has to check a datetime from the datebase, as the datimes stored are GTM+2 and the app is GTM. If I remove the line


Yii::$app->formatter->defaultTimeZone = 'Europe/Madrid';

the Yii::$app->formater->asDate() method returns 12:00 instead of 10:00, it adds 2 hours now.

Any ideas on how to solve it? Changing the values from the database to GTM is not an option.

Thanks

EDIT: One way to show it correctly is using the date() function instead, like this




date('d/m/Y H:i', strtotime($model->date))



I guess I’ll have to edit the whole app.

database column must be DATETIME not TIMESTAMP

or i don’t get what you are doing

Who said the column is a timestamp?

Read the post, every step is explained.