Mysql Query to choose current date on Specifc time zone - mysql

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;

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

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.

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

mysql time_zone not used?

I'm trying to insert or update entries in a table, to Athens timezone. I'm using a shared hosting so I can't set global server timezone.
When I run this multiple query:
SET time_zone="Europe/Athens";
SELECT NOW();
I get the desired Athens time, but when I run something like:
SET time_zone="Europe/Athens";
UPDATE `db`.`tbl` SET `the_time` = NOW() , `foo` = '1' WHERE `tbl`.`id` = 100;
the time set the updated entry is still the server's time!
Why is this happening and how can I fix this?
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. The current time zone is available as the value of the time_zone system variable.
http://dev.mysql.com/doc/refman/5.6/en/datetime.html

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.