How do I get the current time zone of MySQL?
How do I get the current time zone of MySQL?
Anyone knows if there is such a function in MySQL?
UPDATE
This doesn't output any valid info:
mysql> SELECT @@global.time_zone, @@session.time_zone; +--------------------+---------------------+ | @@global.time_zone | @@session.time_zone | +--------------------+---------------------+ | SYSTEM | SYSTEM | +--------------------+---------------------+
Or maybe MySQL itself can't know exactly the time_zone
used,that's fine, we can involve PHP
here, as long as I can get valid info not like SYSTEM
...
Answer by Pekka ? for How do I get the current time zone of MySQL?
Check out Time Zone support in mySQL and the time_zone
system variable. Does that help?
Answer by T.J. Crowder for How do I get the current time zone of MySQL?
From the manual (section 9.6):
The current values of the global and client-specific time zones can be retrieved like this:
mysql> SELECT @@global.time_zone, @@session.time_zone;
Edit The above returns SYSTEM
if MySQL is set to slave to the system's timezone, which is less than helpful. Since you're using PHP, if the answer from MySQL is SYSTEM
, you can then ask the system what timezone it's using via date_default_timezone_get
. (Of course, as VolkerK pointed out, PHP may be running on a different server, but as assumptions go, assuming the web server and the DB server it's talking to are set to [if not actually in] the same timezone isn't a huge leap.) But beware that (as with MySQL), you can set the timezone that PHP uses (date_default_timezone_set
), which means it may report a different value than the OS is using. If you're in control of the PHP code, you should know whether you're doing that and be okay.
But the whole question of what timezone the MySQL server is using may be a tangent, because asking the server what timezone it's in tells you absolutely nothing about the data in the database. Read on for details:
Further discussion:
If you're in control of the server, of course you can ensure that the timezone is a known quantity. If you're not in control of the server, you can set the timezone used by your connection like this:
set time_zone = '+00:00';
That sets the timezone to GMT, so that any further operations (like now()
) will use GMT.
Note, though, that time and date values are not stored with timezone information in MySQL:
mysql> create table foo (tstamp datetime) Engine=MyISAM; Query OK, 0 rows affected (0.06 sec) mysql> insert into foo (tstamp) values (now()); Query OK, 1 row affected (0.00 sec) mysql> set time_zone = '+01:00'; Query OK, 0 rows affected (0.00 sec) mysql> select tstamp from foo; +---------------------+ | tstamp | +---------------------+ | 2010-05-29 08:31:59 | +---------------------+ 1 row in set (0.00 sec) mysql> set time_zone = '+02:00'; Query OK, 0 rows affected (0.00 sec) mysql> select tstamp from foo; +---------------------+ | tstamp | +---------------------+ | 2010-05-29 08:31:59 | <== Note, no change! +---------------------+ 1 row in set (0.00 sec) mysql> select now(); +---------------------+ | now() | +---------------------+ | 2010-05-29 10:32:32 | +---------------------+ 1 row in set (0.00 sec) mysql> set time_zone = '+00:00'; Query OK, 0 rows affected (0.00 sec) mysql> select now(); +---------------------+ | now() | +---------------------+ | 2010-05-29 08:32:38 | <== Note, it changed! +---------------------+ 1 row in set (0.00 sec)
So knowing the timezone of the server is only important in terms of functions that get the time right now, such as now()
, unix_timestamp()
, etc.; it doesn't tell you anything about what timezone the dates in the database data are using. You might choose to assume they were written using the server's timezone, but that assumption may well be flawed. To know the timezone of any dates or times stored in the data, you have to ensure that they're stored with timezone information or (as I do) ensure they're always in GMT.
Why is assuming the data was written using the server's timezone flawed? Well, for one thing, the data may have been written using a connection that set a different timezone. The database may have been moved from one server to another, where the servers were in different timezones (I ran into that when I inherited a database that had moved from Texas to California). But even if the data is written on the server, with its current time zone, it's still ambiguous. Last year, in the United States, Daylight Savings Time was turned off at 2:00 a.m. on November 1st. Suppose my server is in California using the Pacific timezone and I have the value 2009-11-01 01:30:00
in the database. When was it? Was that 1:30 a.m. November 1st PDT, or 1:30 a.m. November 1st PST (an hour later)? You have absolutely no way of knowing. Moral: Always store dates/times in GMT (which doesn't do DST) and convert to the desired timezone as/when necessary.
Answer by JohnZ for How do I get the current time zone of MySQL?
The query below returns the timezone of the current session.
select timediff(now(),convert_tz(now(),@@session.time_zone,'+00:00'));
Answer by Andrew for How do I get the current time zone of MySQL?
Simply SELECT @@system_time_zone;
Returns PST
(or whatever is relevant to your system).
If you're trying to determine the session timezone you can use this query:
SELECT IF(@@session.time_zone = 'SYSTEM', @@system_time_zone, @@session.time_zone);
Which will return the session timezone if it differs from the system timezone.
Answer by Timo Huovinen for How do I get the current time zone of MySQL?
As Jakub Vrna (The creator or Adminer and NotORM) mentions in the comments, to select the current timezone offset in TIME
use:
SELECT TIMEDIFF(NOW(), UTC_TIMESTAMP);
It will return: 02:00:00
if your timezone is +2:00 for that date
I made a cheatsheet here: Should MySQL have its timezone set to UTC?
Answer by Storm Young for How do I get the current time zone of MySQL?
You just need to restart mysqld after altering timezone of System..
The Global time zone of MySQL takes timezone of System. When you change any such attribute of system, you just need a restart of Mysqld.
Answer by Luca Fagioli for How do I get the current time zone of MySQL?
SELECT EXTRACT(HOUR FROM (TIMEDIFF(NOW(), UTC_TIMESTAMP))) AS `timezone`
This will return the timezone as an integer (eg: -6
), handling positive or negative times (here is where EXTRACT
comes into play: HOUR
function alone returns negative timezones as positive).
Answer by alee for How do I get the current time zone of MySQL?
Insert a dummy record into one of your databases that has a timestamp Select that record and get value of timestamp. Delete that record. Gets for sure the timezone that the server is using to write data and ignores PHP timezones.
Answer by dwaid for How do I get the current time zone of MySQL?
It may be as stupid as this
select timediff(current_time(),utc_time())
as is whole mysql
you won't get directly timezone value this way, but if there were no other way...
@@global.time_zone cannot be used in view as it is a variable - and it returns quite unusable value 'SYSTEM' ( i haven't got why somebody bothered with it )
if you need to use your query in a session with changed time_zone ( by session SET TIME_ZONE = ) you will get that with @@session.time_zone if you query @@global.time_zone you get 'SYSTEM' catch 22
if you try datediff, date_sub, or timediff with now() and utc_time() you'll probably run into conversion issues being silently chown by a server
The worst documentation i have ever seen my not help you either.
Excellent work, everybody!
But the something suggested above will probably work at least with some server versions as is mine (5.5.43-37) hosted solution.
Answer by vladkras for How do I get the current time zone of MySQL?
My PHP framework uses
SET LOCAL time_zone='Whatever'
on after connect, where 'Whatever' == date_default_timezone_get()
Not my solution, but this ensures SYSTEM
timezone of MySQL server is always the same as PHP's one
So, yes, PHP is strongly envolved and can affect it
Answer by Abhinav bhardwaj for How do I get the current time zone of MySQL?
To get Current timezone of the mysql you can do following things:
- SELECT @@system_time_zone; //from this you can get the system timezone
- SELECT IF(@@session.time_zone = 'SYSTEM', @@system_time_zone, @@session.time_zone) //This will give you time zone if system timezone is different from global timezone
Now if you want to change the mysql timezone then: 1. SET GLOBAL time_zone = '+00:00' //this will set mysql timezone in UTC 2. SET @@session.time_zone = "+00:00"; //by this you can chnage the timezone only for your particular session
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment