Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dealing with PHP server and MySQL server in different time zones

For those of us who use standard shared hosting packages, such as GoDaddy or Network Solutions, how do you handle datetime conversions when your hosting server (PHP) and MySQL server are in different time zones?

Also, does anybody have some best practice advice for determining what time zone a visitor to your site is in and manipulating a datetime variable appropriately?

like image 360
Kyle Noland Avatar asked Aug 20 '08 17:08

Kyle Noland


People also ask

How do I fix MySQL time zone?

Option 2: Edit the MySQL Configuration File Scroll down to the [mysqld] section, and find the default-time-zone = "+00:00" line. Change the +00:00 value to the GMT value for the time zone you want. Save the file and exit. In the example below we set the MySQL Server time zone to +08:00 (GMT +8).

How do databases deal with time zones?

Oracle: Use the TIMESTAMP WITH TIME ZONE data type as this can store the timezone of UTC. SQL Server: Use the DATETIMEOFFSET data type, which can store the timezone of UTC. MySQL: Use the TIMESTAMP data type which can include a timezone component.

Does MySQL support timezone?

By default, the time zone for a MySQL DB instance is Universal Time Coordinated (UTC). You can set the time zone for your DB instance to the local time zone for your application instead.

Can we change timezone in database?

Use the ALTER DATABASE SET TIME_ZONE command to change the time zone of a database. This command takes either a named region such as America/Los_Angeles or an absolute offset from UTC. This example sets the time zone to UTC: ALTER DATABASE SET TIME_ZONE = '+00:00';


2 Answers

As of PHP 5.1.0 you can use date_default_timezone_set() function to set the default timezone used by all date/time functions in a script.

For MySql (quoted from MySQL Server Time Zone Support page)

Before MySQL 4.1.3, the server operates only in the system time zone set at startup. Beginning with MySQL 4.1.3, the server maintains several time zone settings, some of which can be modified at runtime.

Of interest to you is per-connection setting of the time zones, which you would use at the beginning of your scripts

SET timezone = 'Europe/London';

As for detecting the client timezone setting, you could use a bit of JavaScript to get and save that information to a cookie, and use it on subsequent page reads, to calculate the proper timezone.

//Returns the offset (time difference) between Greenwich Mean Time (GMT) 
//and local time of Date object, in minutes.
var offset = new Date().getTimezoneOffset(); 
document.cookie = 'timezoneOffset=' + escape(offset);

Or you could offer users the chioce to set their time zones themselves.

like image 148
Željko Živković Avatar answered Sep 30 '22 14:09

Željko Živković


Store everything as UTC. You can do conversions at the client level, or on the server side using client settings.

php - date

mysql - utc-timestamp

like image 34
Joel Meador Avatar answered Sep 30 '22 13:09

Joel Meador