views:

288

answers:

3

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...

+1  A: 

Check out Time Zone support in mySQL and the time_zone system variable. Does that help?

Pekka
There is no useful information in `time_zone`
@user what kind of information are you looking for?
Pekka
I'm looking for time zone info,not `SYSTEM`,that's nonsense,right?
@user can you please spell out in your question what info you are exactly looking for (the server's time zone? The client's?) and what tools are at your disposal? You mention PHP. Can you access the command line?
Pekka
@user also, I find you're coming across as a bit of a dick downvoting answers from people who are trying to be helpful, and not exactly wrong. I don't mind the vote nor the loss of point, but your attitude is not exactly encouraging to look any further for a solution.
Pekka
The tools are PHP and MySQL,I want to get valid information like `GMT+8` or something like that,a value between `-12~+12`,but just not `SYSTEM`.I'll upvote and ticket it as accepted once the answer is correct,but sorry will downvote if not the case.
@user as I said, your attitude is not motivating me to research for further answers for you, especially in light of the meagre information you provide. Sorry.
Pekka
@user198729: Wow, lose the attitude. Pekka's the kind of guy to really help people out. If courtesy alone isn't sufficient to motivate you, try self-interest.
T.J. Crowder
Just my thoughts: I wouldn't have taken offense if user had done the same thing to my post. I mean, deadline pressure or frustration does add some rough edges around people, but I think we should allow it to some extent. Maybe I am too forgiving :|
Senthil
@Pekka,sorry,let me take back the downvote.Oops,I can't unless it's edited..
@user198729: I've just edited his answer, you should be able to do so now. (Not that, again, the reputation was the point for him.)
T.J. Crowder
@ T.J. Crowder ,done,thx!
Cheers @T.J. @user, T.J. already posted what I would have looked for next, the PHP functions to get the current time zone. That should do. Another way to get the correct zone would be to query it on the (Linux) command line using `date`: http://www.linuxquestions.org/questions/linux-general-1/how-to-get-time-zone-information-from-the-system-230064/ if you can access that programmatically.
Pekka
+2  A: 

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.

T.J. Crowder
It just returns `System` which is meaning less for me..
@user198729: It's not *meaningless*, though it's not nearly as helpful as I'd hoped. What it means is: Ask the OS, MySQL is slaving to that.
T.J. Crowder
+1 for fine background info!
Pekka
Just curious, why do we have to set the time zone ourselves and then retrieve it? That kind of defeats the purpose doesn't it? I mean, I'd like to ask MySQL for the timezone because I don't know the answer. Maybe I am confused and got things wrong. Can someone explain?
Senthil
@Senthil The mySQL `DATETIME` type does not contain timezone info. Therefore, I think the intended underlying philosophy here is for mySQL to be as timezone blind as possible - which means for the user to stick with one time zone, either UTC or the time zone the server is in, store everything in that zone, and do any conversions on application level or using `CONVERT_TZ()` (http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_convert-tz) At least, that's how I always understood how it's supposed to work, looking at the sparse options mySQL provides in this field.
Pekka
Thanks but this still isn't solved yet,how do I get the tz info by the result of `now()` in PHP?
I totally agree with the "stick to one timezone when saving timestamp" part.. But this-> "..or the timezone the server is in.." how do you find that out? Let's say you saved all the timestamp values as UTC. But your server is in someother timezone and you want to show the timestamp values according to **that** timezone. So how will you know which timezone your server is in? Only then you can pass on some value to CONVERT_TZ() to convert it right?
Senthil
@user198729: I've just updated the answer, but again, the best way frankly is to set the timezone yourself on your connection (as the answer already said), since all that's affected is the functions you'll be calling, *not* data stored in the server.
T.J. Crowder
I hope the answer to my question is not - **don't you know which timezone your server is in? ask your hosting provider!**..
Senthil
@Senthil: You should *never* store datetime values in the database in anything other than GMT, unless you *also* store the timezone it's using alongside the value. So by and large, you don't care what timezone the server is using. About why you can't query it, I'll *speculate*: `SYSTEM` basically means that MySQL asks the OS for the current local time; it probably doesn't actually know what timezone the OS is using. Although it would be possible for the MySQL developers to query the TZ of the OS and report that, it looks like they just didn't think it was important enough to bother.
T.J. Crowder
@T.J. Crowder , I can't assume the `SYSTEM`,that's like hardcode. I'd like to see how to get tz from the `now()` in PHP,but don't know if there is such a function.
@user198729: I never said anything about assuming things (which, agreed, is a very bad idea). I think if you go back and re-read my answer thoroughly, you'll see how to get that information (to the extent you can) and why you probably don't want to (because it tells you nothing about the dates stored in the DB).
T.J. Crowder
Hmmm... your speculation came close to my expected answer in terms of silliness... "just didn't think it was important enough to bother" :D
Senthil
Not exactly,I think the possible solution is to compare `now()` in MySQL and `time()` in PHP,right?
@Senthil: (Re silliness) Yeah. :-)
T.J. Crowder
@user198729: I've just added further discussion to the end of the answer, I'm off, good luck with it.
T.J. Crowder
I decide to accept this answer,though it's not a general solution that can handle all cases,but it DOES solve my specific case:)
A: 

The bleow return the timezone of the current session.

select timediff(now(),convert_tz(now(),@@session.time_zone,'+00:00'));

JohnZ