Home
 

Customer Support

Search for keywords:

Browse by category:

How do I change MySQL timezone?

The MySQL timezone is set to MST (-7 hours GMT/UTC) and is not configurable by you. MySQL is only capable of having 1 timezone setting per mysql daemon. Therefore, you cannot select NOW() and expect a result in a timezone other than MST.

However, there are ways for you to get results that are in your preferred timezone. First determine how many hours your desired timezone is off from MST. For example, EST is +2 hours. PST is -1 hour.

Knowing the time offset, you can replace all your SQL statements of

SELECT NOW();


with

SELECT DATE_ADD(NOW(), INTERVAL 2 HOUR);


which will give you an EST date result. For a result in PST, you would do:

SELECT DATE_SUB(NOW(), INTERVAL 1 HOUR);


If you are working with time in seconds instead of dates, then factor in the offset in seconds. Because there are 3600 seconds in an hour, and EST is 2 hours later than MST, the following converts timestamps from MST to EST:

SELECT unix_timestamp() + (3600 * 2);

SELECT FROM_UNIXTIME(UNIX_TIMESTAMP() + (3600 * 2));


See the MySQL Manual's Date and Time Functions for more information.

Depending on your application, you may also need to do one of the following (but not both):

1. Find every place in your code where a date or time is displayed to the browser and have a user defined function change it to add or subtract the appropriate number of hours before displaying it.

2. Find every place in your code where dates or times are input into your system and have a user defined function add or subtract the appropriate number of hours before storing it.

If you were looking for information about changing PHP's timezone, look here.


User-Contributed Notes

add a note
15-Jan-2004 13:32
If MySQL had a user definable environmental variable for timezone so 
that you could issue a SQL query like: "SET SESSION timezone=EST" then
that would solve mysql timezone problems among different users. As of
4.x though, this does not exist.

luap -at- icculus.org
07-Mar-2004 15:07
the unix_timestamp() example is incorrect, since the unix timestamp
should *always* be GMT (seconds since '1970-01-01 00:00:00' GMT).

http://www.mysql.com/documentation/mysql/bychapter/manual_Functions.html
#IDX1363

07-Mar-2004 16:30
The unix_timestamp() example is NOT incorrect because the MySQL server
time is NOT in GMT.

19-Mar-2004 15:05
There seems to be a lot of confusion about this, even this article is
not correct.  I hope this will clear things up for some of you.

1) Always store the accurate date (in your time zone) within the
database.  Do not try adding or subtracting hours to compensate for
MySQLs MST default setting.  If you do this it can throw off queries
when you are searching by date.  
2) UNIX_TIMESTAMP is always GMT.  This is the heart of the problem since
the database is converting the dates (from whatever time zone MySQL is
set to) to GMT time.  Unix timestamps are always the number of seconds
since 1970 GMT! 
3) MySQL has its own time zone setting that can be configured.  I added
the following lines to "my.cnf" I added this as root but each MySQL user
also has their own ".my.cnf" that can be used to override the global
settings.  If you do not find "[mysqld_safe]" in brackets already in
my.cnf you may need to add it.  The following time zone setting is PST
on my system, but it my be different on yours. On linux I found the
available time zones in the file /usr/share/zoneinfo/zone.tab
-------------------
[mysqld_safe]
timezone = America/Los_Angeles
---------------------------


4) This is the case in PHP as well.   The PHP function TIME() will
return a GMT... number of seconds since 1970.   It is always GMT.   When
you run the PHP function DATE() it will convert the Unix timestamp into
whatever time zone you are in.  So make sure to set the time zone on
your PHP system.   I added the following line to a constants file that
gets included on every PHP script on my system... but you can configure
it in php.ini as well.
-------------------------
putenv ('TZ=America/Los_Angeles');

5) You should also set the time zone correctly on your server.   You can
check by typing "date" at the shell prompt and see what it says.   This
is not super important for web development though because everything is
abtracted through Unix timestamps which are GMT.   The basic rule...
each application needs its own time zone setting so it knows how to do
the conversions.   
6) For really fancy applications where every user sees dates shown in
their own time zone (based on their login)... you should set the time
zone in MySQL to GMT and store the dates within the DB as GMT as well.  
 Then abstract a PHP function to modify dates before an insert, similar
to what this article mentions.    It is very important that the time
zone setting in mysql is set to GMT if you go this route.   Then modify
the time zone setting in the PHP environment based upon login. 

26-Sep-2005 13:27
UNIX_TIMESTAMP() will return the seconds since 1970-01-01 00:00:00 GMT. 
However, UNIX_TIMESTAMP('1970-01-01 00:00:00') will assume the datetime
is in local time, not GMT.  I.e., for EDT servers, it will report it as
18000 seconds.

Anonymous -at- user
23-Feb-2006 12:15
The previous note is correct: UNIX_TIMESTAMP assumes that the value in a
DATETIME field is local time - that is, the time set by the "SYSTEM"
time variable. However, here's a workaround I've found:

SELECT UNIX_TIMESTAMP (
CONVERT_TZ (my_datetime_column, '+0:00', 'SYSTEM'));

CONVERT_TZ converts the value of "my_datetime_column" to the system's
local time; UNIX_TIMESTAMP converts it back to Greenwich time, and
returns the time in UNIX epoch time.

This is a little convoluted, but it works.

maninder123 -at- yahoo.com
29-May-2006 01:37
if using mysql >=4.1.1 you can use UTC_TIMESTAMP() and UTC_DATE() to get
GMT times and dates.

add a note

Related Questions:


How do I import delimited data into MySQL?

How do I change my MySQL password?

How do I import a MySQL dumpfile into my database?

How can I export data in CSV or tab delimited format?

How do I connect to my MySQL database?

Why do i get a query syntax error 1064 from MySQL when the syntax seems correct?

How do I export and move my database tables between servers or copy databases?

I can't connect to local MySQL server through socket 'mysql.sock'

How do I create a .my.cnf MySQL preference file?

What is my database server name?

What is my MySQL Username or Database Name?

MySQL says I have too many connections.

How do I check how much disk space my database is using?

A MySQL MYI table has errors in it.

Can I connect to MySQL remotely?

Can I connect to my MySQL database from my own computer?

I can't get phpMyAdmin to work.

phpMyAdmin gives SQL syntax error when I try to create table.

How can I use odbc_connect() in my PHP scripts to connect to MySQL?

What kind of database server do you offer?

How many databases do I get?

What am I allowed to do with my MySQL database?

Will we be able to do our own MySQL admin?

How do I import a MySQL dumpfile into my database via phpMyAdmin?

Do you offer MySQL 5?

How do I export a MySQL dumpfile via phpMyAdmin?

Browse Categories:

Getting Started, FTP, Telnet/SSH, Moving Domains, E-mail, Traffic Reports, Mailing Lists, Apache, PHP, CGI, Other Server-Side Scripting, MySQL Database, Imaging Libraries, Other Software, Billing & Terms, Control Panel, E-commerce, Pre-Sales


Tiny Modwest Logo         Copyright 2000-2008 by Modwest, Inc.          About Us    |    Blog    |    Jobs    |    Web Design    |    Contact Us