Timezone Question

Hi there,

I just wonder if there is a solution to solve problem with different time zones, because every single user has different time zone (possible).


timeZone' => 'America/Chicago'

in my main-local.php it shows data correctly in grid, but it does not show in my inputs.

I just tried with:

'on afterOpen' => function($event) {

    $event->sender->createCommand("SET TIME ZONE 'America/Chicago'")->execute();


But it does not work… Thanks.

PS: I’m using POSTGRES

I’m using UTC+0 at PHP side + storing everything in unix timestamps. Converting to timezone I need when displaying it.

So I need to convert everywhere when displaying data in input fields. There is no framework opportunity to solve this efficiently? If thats true, the only thing I need to do it to make select queries to return in desired time zone or get in UTC (as I currently get) and then convert it with PHP.

Since the conversion usually depends on the application (all users with the same timezone, automatic detection, pre-selected timezone in profile etc.) I’m doing it manually.

If you’ll look at Yii formatters there are methods for it.

Me I do this in my config (config.web.php) :


'formatter' => [

  'class' => 'yii\i18n\Formatter',

  'timeZone' => 'Europe/Paris',

  'defaultTimeZone' => 'Europe/Paris'



And then I use Yii::$app->formatter to format the data

echo Yii::$app->formatter->asDate($model->date, 'long')

Thanks for the answer. Well, that’s fine. defaultTimeZone property is available since 2.0.1. The problem is query.

My table:

  • event -

id start_time end_time

1 2015-01-01 03:00:00 2015-01-01 04:00:00

2 2015-01-01 14:00:00 2015-01-01 15:00:00

My query looks like this:

SELECT ABS(EXTRACT(EPOCH FROM end_time - start_time))/60 as time_length, start_time at time zone 'America/Chicago' as start_time, end_time at time zone 'America/Chicago' as end_time FROM event

 (("start_time" >='2015-01-01 00:00:00' AND "start_time"<='2015-01-01 23:59:59') OR ("end_time">='2015-01-01 00:00:00' AND "end_time"<='2015-01-01-01'))

ORDER BY start_time ASC;

It returns both rows and it should not, because time zone iz America/Chicago. First row started in 2014-12-31.

PS: Its postgreSQL.


How to change time zone dinamically? I have HelperModel and there is something like this:

 public static function getTimeZone(){

        return "America/Chicago";

        // I will get it from db


Anyway, I’d like to know why:

'timeZone' => \common\models\HelperModel::getTimeZone()

does not work.