Yii2 formatter not working as expected

I have a timestamp column in mysql database. This is, how I can do some date math with it, which works fine:

    // Konverzia pomocou datetime bez problemov ..
$braTime = new DateTime($model->end, new DateTimeZone('America/Sao_Paulo'));
var_dump($braTime); // 2018-10-26 23:59:59
$utcTime = $braTime->setTimezone(new DateTimeZone('UTC'));
var_dump($utcTime); // 2018-10-27 02:59:59
$backBraTime = $utcTime->setTimezone(new DateTimeZone('America/Sao_Paulo'));
var_dump($backBraTime); // 2018-10-26 23:59:59 

I want to do the same with Yii::$app->formatter, but no matter what I do, I can’t make it work:

$f = Yii::$app->formatter;

var_dump('-----------------------------------------------------------------------------');
// This is the default applications settings

var_dump($f->defaultTimeZone);     // Europe/Prague
var_dump($f->timeZone);            // America/Sao_Paulo
var_dump($model->end);             // 2018-10-26 23:59:59
var_dump(strtotime($model->end));  // 1540609199 => according to https://www.unixtimestamp.com/index.php, 1540609199 translates to 10/27/2018 @ 2:59am (UTC)
var_dump($f->asDateTime(strtotime($model->end))); // 27.10.2018 00:59:59 ???????????? The datetime expects a UTC UNIX timestamp, so why is the conversion wrong? I expect 2018-10-26 23:59:59

var_dump('-----------------------------------------------------------------------------');
// Here I try to override the timezones used by the formatter

$f->defaultTimeZone = 'UTC';
$f->timeZone = 'America/Sao_Paulo';
var_dump($f->defaultTimeZone);     // UTC
var_dump($f->timeZone);            // America/Sao_Paulo
var_dump($model->end);             // 2018-10-26 23:59:59
var_dump(strtotime($model->end));  // 1540609199 => according to https://www.unixtimestamp.com/index.php, 1540609199 translates to 10/27/2018 @ 2:59am (UTC)
var_dump($f->asDateTime(strtotime($model->end))); // 27.10.2018 00:59:59 ???????????? The datetime expects a UTC UNIX timestamp, so why is the conversion wrong? I expect 2018-10-26 23:59:59

What I want to achieve is, I want to print the $model->end ‘timestamp’ using the formatter, but I can’t make this work.

Thanks a lot for your help, I already spend few days with this problem and I can’t find a proper way to deal with this issue.

It looks fine for me.

You provide 1540609199 for asDatetime() - this timestamp is 2018-10-27 02:59 (UTC) as you stated correctly.
Your formatter is set to America/Sao_Paulo which is UTC-2 currently.
The output must be 2018-10-27 00:59 and it is.

Hi Pawel, thank you for your review, I appreciate it a lot!

It looks like you are right … I was in the impression, that the time difference between America/Sao_Paulo and UTC was -3hod, but it looks like it’s -2hod at the moment. I was confused initially and it looks like everything works as expected!

Again, thank you for your input!