Save date in America Pacific timezone format in MySQL - mysql

I'm trying to save date in the America Pacific timezone format. I execute before querying the following query
SET time_zone='-09:00'
but when I close the connection and reopen it then the date seems in the local format. Do you know why?

Per MySQL Documentation on Time Zone Support
SET time_zone = timezone; is a per connection setting and so when connection resets, it's resets to default value.
Per-connection time zones. Each client that connects has its own time
zone setting, given by the session time_zone variable. Initially, the
session variable takes its value from the global time_zone variable,
but the client can change its own time zone with this statement:
mysql> SET time_zone = timezone;
If you want to set it globally then you can either
Change the configuration file (.cnf file) to reflect the change
(OR)
If you have SUPER privilege then you can set the global server time zone value at runtime with the command below
SET GLOBAL time_zone = timezone;

You'll need to edit your my.cnf and reboot to set it permanently.
essentially you'll want to add this line to /etc/my.cnf
default_time_zone='-09:00'
See these articles for more details.
How do I set the time zone of MySQL?
http://dev.mysql.com/doc/refman/5.5/en/server-system-variables.html#sysvar_time_zone

Related

Timestamp from server is different than local

I have a createdAt column of type datetime, and an updatedAt column of type timestamp in a table in our mysql database. In the database the dates are correct. I query my database and display the values in React, and they show correctly when ran from my local xampp server.
Our beta and live sites are hosted on an external debian-jessie server, and on both of these the updatedAt that gets displayed in React is in an entirely different timezone, with a 7 hours difference. If I run "date" on our server it shows the correct timezone, and I know for a fact the server is hosted in our timezone.
If it matters at all, we use typeORM/GraphQL/type-graphql to query data and Apollo to get it to React.
Does anyone have an idea of why this happens, or a solution?
Your local time zone may differ from your server's MySQL time zone. Ideally, MySQL time zone should be the same as your own to handle data more efficiently.
Your timezone might be set at one of these places in mysql:
In the file my.cnf file in the [mysqld] section
default-time-zone='+00:00'
global.time_zone variable
To see what value they are set to:
SELECT ##global.time_zone;
To set a value for it use either one:
SET GLOBAL time_zone = '+8:00';
SET GLOBAL time_zone = 'Europe/Helsinki';
SET ##global.time_zone = '+00:00';
##session.time_zone variable
SELECT ##session.time_zone;
To set it use either one:
SET time_zone = 'Europe/Helsinki';
SET time_zone = "+00:00";
SET ##session.time_zone = "+00:00";

MYSQL SELECT returns timestamp with timezone calculation

I have a TIMESTAMP field in my table.
When I insert a new row with a timestamp value, the value is saved correctly. but when I'm making a SELECT request (via sequelize), I get the timestamps formatted to -2 hours (the difference from my timezone and the UTC timezone)
is there a way to force the SQL to always return the UTC timezone?
From the documentation
https://dev.mysql.com/doc/refman/5.5/en/datetime.html
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.
So the reason you get the time difference when you retrieve is because MySQL converts it to your server's time upon retrieval.
From the documentation again:
https://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html
The initial global server time zone value can be specified explicitly
at startup with the --default-time-zone option on the command line, or
you can use the following line in an option file:
default-time-zone='timezone'
If you have the SUPER privilege, you can set the global server time
zone value at runtime with this statement:
SET GLOBAL time_zone = timezone;
So if you want to change your MySql's server timezone to UTC you should use:
In my.ini file (restart needed)
default-time-zone = '+00:00'
Runtime
(If you want to set the timezone per connection just ommit GLOBAL)
SET GLOBAL time_zone = '+00:00';

Set custom timezone only for return queries in MySQL

Is there any way to get the time from my database at my given timezone only for return query. On SET or UPDATE I want to use the UTC timezone as before.
Already used
SET GLOBAL time_zone = '+6:00';
But it changes my write timezone too.

Mysql Query to choose current date on Specifc time zone

I need to delete entries from my MySql table which is less than current date. I am using this query for that.
delete FROM Offers WHERE ed < DATEADD(dd,-1,GETDATE())
I am working on Windows Azure and my server is running on a different time zone. I am not seeing any option to change my MySql server time zone.
What is the query for calling GETDATE() function on a specific time zone.Or I need to convert current time to a specific time zone (I need gmt+5.30).
Can I get a query something like this , delete FROM Offers WHERE ed < DATEADD(dd,-1,GETDATE()+5.30)
You can set MySQL timezone by using this query
SET GLOBAL time_zone = '+5.30';//Your timezone
SET GLOBAL time_zone = 'Europe/Helsinki';//Your timezone name
SET ##global.time_zone='+00:00';//Your timezone
You can set a different timezone for current session like below. See Documentation for more information.
SET time_zone = timezone_name;
(OR) You can as well set global server timezone using
SET GLOBAL time_zone = timezone_name;

setting timezone for MySQL using PHPMyAdmin

Currently whenever a user creates a new account, I have a Creation_date column that has datatype timestamp. Currently this timestamp reads 3 hours ahead of EST (if it's 5PM in Boston it reads 8PM). Is there a way to change the timezone to EST? What timezone is it set to currently?
This has to do with MySQL's timezone.
You can set it per connection (e.g. via PHPMyAdmin) with the following:
SET time_zone = timezone;
However, this will reset if MySQL restarts. So it is better set at the server level. Which, I assume, you can't.
I encourage you to read more from the MySQL Docs.
Accordingly with this question
SET SESSION time_zone = '+8:00'
TIMESTAMP values are converted to UTC when inserted into the DB and converted back to the current timezone set in MySQL on retrieval. The other answers show how to set the timezone in MySQL which will determine what value comes out of the database.
Another option is to use a DATETIME column, store ALL of your dates in UTC, and convert them to the desired timezone in your application (PHP or wherever you get the values from).
SET GLOBAL time_zone = timezone;
This is a COMPLETE example, for Argentina
SET GLOBAL time_zone = "-03:00";
you just need to change the "-03:00" for your timezone and run that command like a simple query, even in PhpMyAdmin.