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.