Timestamp from server is different than local - mysql

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";

Related

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;

Save date in America Pacific timezone format in 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

Exporting with phpMyAdmin changes timestamps

When exporting a file through phpMyAdmin some timestamps are put back an hour. How may I prevent this? I do not want timestamps tampered with. Here's a screenshot for the curious (see the dates.)
I believe the cause may be the SET time_zone = "+00:00"; that is added to every export file.
Is this suppose to happen? Is it a known bug?
I'm running:
-- Server version: 5.5.37-0ubuntu0.14.04.1
-- PHP Version: 5.5.9-1ubuntu4
The times are not actually being 'tampered with'.
MySQL interally stores TIMESTAMP columns converted to UTC time, then uses a mixture of system and session (client session) values to determine what to display to the user.
You can check both of these values running the following query yourself.
SELECT ##global.time_zone, ##session.time_zone;
So when your PHPMA script generates its dump, its specifying a session time_zone variable so when you run it MySQL will convert them all from that timezone back to UTC. When you then go to import that to another database, it will still convert them back to the UTC values you're expecting.
So to summarise if the values in the dump with SET time_zone = "+00:00"; are all "1 hour behind" the values you see when querying via PHPMyAdmin, this only appears this way because the connection via PHPMyAdmin will have it's timezone one hour ahead of UTC.

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.