Skip to content
Advertisement

Users’ timezone issue onYii2 with PostgreSQL

I would like to ask for a better approach. I am using Yii2 with PostgreSQL, and set all timestamp fields for all table to timestamp with timezone data type.

JavaScript

I have created setting which will be called for every response,

JavaScript

which will return something like Asia/Makassar or Asia/Jakarta (it depends on user’s setting).

I extend the yii18nFormatter, using a class named ZeedFormatter below, and make a new function to display the datetime value in user’s preferred timezone.

JavaScript

The idea is that whenever I need a ‘local’ datetime format, I can call it like so:

JavaScript

Or, if used inside a GridView,

JavaScript

Can I get this functionality only using Yii2’s feature? Or (another option) is calling created_at AT TIME ZONE 'Asia/Makassar' from the Psql query? I hope I can get an enlightment. Thank you.

Advertisement

Answer

My (another) approach is to keep using the asDatetime() function.

I just realized that I need to remove / comment the timezone from config file. If the value is set there, no matter how I set the timezone at other place, Yii always gets the value from the config file (common/config/main.php) if we are using the asDatetime formatter.

JavaScript

If we need another (or customized) format, we can still use the ZeedFormatter as above and add some desired functions. For example :

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement