0

In brief

We want to input value for a timestamp column WITH timezone value in the literal string e.g. '201708-09 10:11 GMT +07:00'

In full

We have a sample table

create table some_table (
  start_time timestamp default current_timestamp
);

We can insert this value

into some_table values('2017-08-09 10:11');

but this will fail

into some_table values('2017-08-09 10:11 GMT +07:00');

How can we set timezone value for that column when insert?

2

In MySQL any date/time values inserted into timestamp are automatically converted and stored as UTC.

To quote MySQL documentation:

MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. (This does not occur for other types such as DATETIME.) By default, the current time zone for each connection is the server's time. The time zone can be set on a per-connection basis. As long as the time zone setting remains constant, you get back the same value you store. If you store a TIMESTAMP value, and then change the time zone and retrieve the value, the retrieved value is different from the value you stored. This occurs because the same time zone was not used for conversion in both directions.

So the answer to your question would be to set the time_zone value for your connection to the database by setting the session variable called time_zone before inserting/updating the value into the database and do the same before fetching the value back from DB. By doing so, the conversion to the actual value of date/time to reflect the Timezone will be automatically handled by MySQL.

This is how you can set the session Timezone on MySQL Client:

mysql> SET time_zone = timezone;

To set global Timezone on MySQL Client:

mysql> SET GLOBAL time_zone = timezone;

There will be ways to do this in the Programming Language you are using.

More details from MySQL manual here:

https://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html

  • In short, what you try to say is that MySQL not support timezone in literal, right? – Nam G VU Mar 28 '17 at 15:37
  • The timezone of the literal value will be the one set by mysql 's timezone value – Nam G VU Mar 28 '17 at 15:38
  • 2
    @NamGVU, yes as far as I know, timezone in a literal is not recognised as a valid date by MySQL. – thatsaru Mar 28 '17 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.