Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: Get local time for a specific time zone

Tags:

timezone

mysql

Here's a simple version of the table users:

+--------------+-------------------+
|      id      |     timezone      |
+--------------+-------------------+
|       1      | 'Europe/Helsinki' |
|       2      |  'Europe/Paris'   |
+--------------+-------------------+

I want to know what's the local time for each one of these users (depending on their time zones), so that I can select users for who it's 4pm for example.

I'm using the LAMP stack, but I'd like to do that using MySQL only (not selecting all users and running them in a PHP loop).

like image 351
aaaaaa Avatar asked Sep 21 '12 12:09

aaaaaa


People also ask

How do I get MySQL time zone?

The following is the syntax to get the current time zone of MySQL. mysql> SELECT @@global. time_zone, @@session.

Does MySQL support timezone?

To explicitly specify the system time zone for MySQL Server at startup, set the TZ environment variable before you start mysqld. If you start the server using mysqld_safe, its --timezone option provides another way to set the system time zone. The permissible values for TZ and --timezone are system dependent.

Does MySQL use UTC?

Internally a MySQL timestamp column is stored as UTC but when selecting a date MySQL will automatically convert it to the current session timezone. When storing a date in a timestamp, MySQL will assume that the date is in the current session timezone and convert it to UTC for storage.

Does MySQL timestamp have timezone?

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.


2 Answers

You can change the timezone with set time_zone:

mysql> set time_zone='Europe/Helsinki';
mysql> select now();
2012-09-21 16:15:06
mysql> set time_zone='Europe/Paris';
mysql> select now();
2012-09-21 15:15:40

Using this you can, for example, define a function that returns the current time for the user's timezone:

create function current_time_in_tz(tz varchar(40)) returns datetime 
begin
set @old_tz = @@session.time_zone;
set time_zone=tz;
set @now = now();
set time_zone=@old_tz;
return @now;
end

select id, current_time_in_tz(timezone) from users;

Note that DATE, TIME and DATETIME values don't depend on the time zone, so values from columns of these types are not automatically adjusted when querying. TIMESTAMP values are adjusted:

mysql> create temporary table tbl (dt datetime, ts timestamp);
mysql> insert into tbl values (now(),now());
mysql> select * from tbl;
+---------------------+---------------------+
| dt                  | ts                  |
+---------------------+---------------------+
| 2012-09-21 15:21:56 | 2012-09-21 15:21:56 |
+---------------------+---------------------+
mysql> set time_zone='Europe/Helsinki';
mysql> select * from tbl;
+---------------------+---------------------+
| dt                  | ts                  |
+---------------------+---------------------+
| 2012-09-21 15:21:56 | 2012-09-21 16:21:56 |
+---------------------+---------------------+

If set time_zone fails with this error:

ERROR 1298 (HY000): Unknown or incorrect time zone: 'Europe/Helsinki'

you need to load the time zone info into mysql with a command like this:

mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql

For more info see http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html

like image 54
Joni Avatar answered Nov 15 '22 20:11

Joni


Use the CONVERT_TZ for this: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_convert-tz

SELECT * FROM users WHERE hour(CONVERT_TZ(now(), server_tz, `timezone`))=16
like image 27
Nin Avatar answered Nov 15 '22 18:11

Nin