How to set MySQL to use GMT in Windows and Linux - mysql

I'm just trying to get MySQL to store time in GMT...
I've read the documentation here: http://dev.mysql.com/doc/refman/5.1/en/time-zone-support.html
It says to set: default-time-zone='timezone' in an option file.
However, I've googled for several different terms and cannot find possible values "timezone" is supposed to be. I also don't know where in my.ini (and in Linux, my.cnf) to put this directive. Below [msyqld] ?

Go to [mysqld] section of your my.ini file and under that section enter this line
default-time-zone = '+00:00'
'+00:00' indicates the offset from GMT which in your case will be 0. Please note the '+' sign in the string.
You don't need to install any timezone tables for your problem. After restarting the server, your server will operate in the UTC timezone and hence NOW() will give you the time in GMT.
By default, MySQL is set to your SYSTEM timezone ie. your server timezone is same as your system timezone. So you could also change your system time zone to solve your problem, though this is not recommendable.

Just to save a few clicks, the list of zone names is shown like this:
select name from mysql.time_zone_name;
Beware of setting a fixed-offset zone such as 'GMT' or '+00:00' since it will not alter to match local time / DST etc. If you want it to follow local time, set the zone to 'Europe/London' instead. If you don't want that I'd go for UTC over GMT anyway.

The MySQL Timezone table is not loaded by default, which might be why you're experiencing difficulties. You need to load the timezone table and then set your timezone using the instructions above.

I use this for GMT+2:
SET GLOBAL time_zone = '+02:00';

Screw this... DATETIME, although much easier on the eye when viewing tables, isn't worth this nightmare.
I'm just going to use TIMESTAMP.

Yes, that's a server option for mysqld, and so should be put in the [mysqld] section.
Also, regarding values, see The Definitive Guide to MySQL 5

Related

How to change database timezone from UTC to local in wordpress

I'm using a custom plugin in wordpress. All date and time are stored in database as UTC. I saw in posts table, there're 2 columns for UTC and local timezone (post_date, post_date_gtm), but in this plugin table, there's only 1 column to store the created date as UTC.
Currently, I'm using sql query and $wpdb to show the data between "start date" and "end date".
Please help me to show them in local timezone (That is setted in general setting) instead of UTC!
Thanks and sorry about my English.
Assume your local time zone is +05:00, use the following condition:
WHERE CONVERT_TZ(col_date_utc, '+00:00', '+05:00') BETWEEN '....' AND '....'
I thought this might be useful
There are 3 places where the timezone might be set in MySQL:
Check here link
Have you tried Admin> Settings> Timezone?

Laravel 5 timestamp not update right time

After inserted record into database, created_at timestamp not display right time, it late 2h from server.... Also If i type into mysql SELECT NOW() it show right time, any idea what is problem?
Edit..
It take date from Carbon class... any idea how to change it?
The default timezone for laravel is UTC which is located in config/app.php file.
If you want to change the timezone to your preferred timezone, choose your preferred timezone from this list or from this list and replace the UTC with your chosen timezone.
A few notes. As per the comments here, to be precise, the last 3 comments: You should not change the default values.
Storing dates of different timezones in data source of a same
application (by changing the timezone in config for current user &
letting Laravel handle it from there on) is just asking for trouble &
is a bad design. You will lose data integrity. What will happen when a
user changes timezone? You'll update all the dates in the database for
that user?
Store dates for all users as UTC (or any other timezone of your
choosing, just select one & stick to it). Laravel already uses the
excellent Carbon library, use it to convert dates from your
timezone (in which they're stored in DB) to users' timezone (which
you would store with in every user's settings) when you display the
dates.
For the other people that also still have the wrong date/time after changing the config file:
My problem was in php in my vagrant box (Homestead).
To solve it I did:
First ssh into you vagrant box and then run this in the command line:
date
This should return something like "Mon Jul 3 13:48:52 CEST 2017". Check if this date/time is correct. If not do the following:
sudo ntpdate -b pool.ntp.org
This should update your system time. Check it again with the first command. If it is still not write you probably have to change your systems timezone. You can do this by running:
sudo rm -f /etc/localtime
sudo ln -sf /usr/share/zoneinfo/Europe/Amsterdam /etc/localtime
You can use any timezone you wish, I prefer "Europe/Amsterdam".
You can get other timezones here

Date value in mysql tables changes while exporting mysql db

I am exporting mysql table to setup it on live, but while exporting DB I noticed that my date column value is changing.. If it was "2007-06-11 00:00:00" earlier then after export it is now changed to "2007-06-10 18:30:00",
why this is so?
anybody have idea about this?
Bug #13052 existed in versions of MySQL prior to 5.0.15, in which dump files expressed TIMESTAMP columns in the server's timezone but did not include a SET TIME_ZONE command to ensure anyone (or any subsequent server) reading the dump file understood that; without such a command, receiving servers assume that any TIMESTAMP values are in its default timezone.
Therefore a transfer between servers in timezones offset by 18:30 (e.g. from South Australia to California) would lead to the behaviour you observe.
Solutions to this problem, in some vague order of preference, include:
Upgrade the version of mysqldump on the original server to 5.0.15 or later (will result in the dumpfile expressing all TIMESTAMP values in UTC, with a suitable SET TIME_ZONE statement at the start);
Prior to export (or import), change the global time_zone variable on the source (or destination) server, so that it matches the setting on the other server at the time of import (or export):
SET GLOBAL time_zone = 'America/Los_Angeles'; -- ('Australia/Adelaide')
UPDATE the data after the fact, applying MySQL's CONVERT_TZ() function:
UPDATE my_table
SET my_column = CONVERT_TZ(
my_column,
'America/Los_Angeles',
'Australia/Adelaide'
);
If using either solution 2 or solution 3, beware to use the exact timezone of the relevant server's time_zone variable, in such a manner as to include any daylight savings time. However, note that as documented under MySQL Server Time Zone Support: "Named time zones can be used only if the time zone information tables in the mysql database have been created and populated." The article goes on to explain how to create and populate the time zone information tables.
before export database just follow below steps:
export with custom option
uncheck the checkbox below
Dump TIMESTAMP columns in UTC (enables TIMESTAMP columns to be dumped and reloaded between servers in different time zones)
show in below image

MySQL DEFAULT CURRENT_TIMESTAMP doesn't work with timezones?

I'm having a bizarre issue with my local MySQL server. I have a table with a created_at timestamp column set to CURRENT_TIMESTAMP by default. Everything I've read indicates that this should store the time in UTC, then convert it back to local time for display. However, it's not working right. I'm not sure if it's screwing up on the display or what, but when I read the timestamps out, it's giving me the GMT times instead of adjusting for my local timezone. This wouldn't be as big of an issue, except that NOW() is giving me local time, so I can't compare them properly. I feel like I'm probably missing something stupid, but what's going on here?
If it matters, I'm running MySQL 5.5.24 under 64-bit Windows 8 (unfortunately).
Edit:
Poking at it some more has revealed that the timestamps are working fine when I add rows manually; it's just the rows coming from my PHP that are screwy. I guess the PHP connection must be overriding the default timezone for some reason.
Have you loaded time zone data?
http://www.geeksengine.com/article/populate-time-zone-data-for-mysql.html
Ah-hah! My PHP server and my MySQL server were using different timezones. Needed to go add date.timezone = America/New_York to my php.ini file.

Set MySQL database timezone to GMT

I need to change the timezone of a single database is this possible?
I know we can change the timezone within our WHM (we are using a dedicated server from hostgator), however a large number of legacy software running on the server has a lot of +6 hours coding in it (i.e. server timezone is CST, we wanted the time in GMT so previous developers altered date/times manually within the code - bad!).
I am now working on a new software and would like to have it all in GMT, I know I can use date_default_timezone_set('GMT') however that will not solve MySQL inserts where the datetime column is set to CURRENT_TIMESTAMP as it will insert # CST timezone.
No, it's not possible to change the timezone for a single database within a MySQL instance.
We can retrieve the server and client time_zone settings with a query, like this:
SELECT ##global.time_zone, ##session.time_zone;
We can also change the client timezone for a session, or change the timezone for the entire MySQL instance.
But we need to be keenly aware of the implication that this change will have on existing client connections, and the how DATETIME and TIMESTAMP values already stored in the instance will be interpreted.
To have the server time_zone set at MySQL instance startup, we can modify the /etc/my.cnf file (or wherever the mysql instance initialization parameters are read from), under the [mysqld] section:
[mysqld]
default-time-zone='+00:00'
-- or --
It is also possible (less desirable) to add the --default_time_zone='+00:00' option to mysqld_safe
NOTE: Changing the timezone setting on the MySQL server does NOT change the values stored in existing DATETIME or TIMESTAMP columns, BUT since it does effectively change the context in which those stored values are interpreted, it will look like all of the values ARE shifted. (Where 08:00 was taken to mean 8AM CST, with the time_zone of the server changed from CST to GMT, that same '08:00' will now be taken to be 8AM GMT, which would effectively be 2AM CST.
Also keep in mind that TIMESTAMP columns are always stored in UTC, while DATETIME columns do not have a timezone.
http://dev.mysql.com/doc/refman/5.5/en/datetime.html
Each client session can change the timezone setting for their own session:
SET time_zone='-06:00';
But none of this really "solves" the timezone conversion problem, it just moves the conversion problem around.
There's nothing inherently "bad" with the application layer handling timezone conversions; sometimes, that's the best place to handle. It just has to be done correctly and consistently.
(What's odd about the setup you describe is that the app is storing DATETIME values as if the MySQL server time_zone is set to GMT, but the MySQL server time_zone is set to something else.)
If you can't change your current time zone you can change the result.
date 2015-01-05 12:06:16
Select date + Interval 2 Hour 'date' From ExampleTable
date 2015-01-05 14:06:16
You can modify the the default value instead of entering current_timestamp make it insert current_timestamp added to hours offset of your timezone. I just did it this way when didn't found any solution, had to invent my own.