<p>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?</p>

<p>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?</p>

[{'answer_id': 18602, 'author': 'Joel Meador', 'author_id': 1976, 'author_profile': 'https://Stackoverflow.com/users/1976', 'pm_score': 4, 'selected': False, 'text': '<p>Store everything as UTC.  You can do conversions at the client level, or on the server side using client settings.</p>\n\n<p><a href="http://us.php.net/date" rel="noreferrer">php - date</a></p>\n\n<p><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_utc-timestamp" rel="noreferrer">mysql - utc-timestamp</a></p>\n'}, {'answer_id': 18607, 'author': 'Željko Živković', 'author_id': 1926, 'author_profile': 'https://Stackoverflow.com/users/1926', 'pm_score': 5, 'selected': True, 'text': '<p>As of PHP 5.1.0 you can use <a href="http://www.php.net/manual/en/function.date-default-timezone-set.php" rel="noreferrer"><em>date_default_timezone_set()</em></a> function to set the default timezone used by all date/time functions in a script. </p>\n\n<p>For MySql (quoted from <a href="http://dev.mysql.com/doc/refman/4.1/en/time-zone-support.html" rel="noreferrer">MySQL Server Time Zone Support</a> page)</p>\n\n<blockquote>\n  <p>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. </p>\n</blockquote>\n\n<p>Of interest to you is per-connection setting of the time zones, which you would use at the beginning of your scripts</p>\n\n<pre><code>SET timezone = \'Europe/London\';\n</code></pre>\n\n<p>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.</p>\n\n<pre><code>//Returns the offset (time difference) between Greenwich Mean Time (GMT) \n//and local time of Date object, in minutes.\nvar offset = new Date().getTimezoneOffset(); \ndocument.cookie = \'timezoneOffset=\' + escape(offset);\n</code></pre>\n\n<p>Or you could offer users the chioce to set their time zones themselves.</p>\n'}, {'answer_id': 18626, 'author': 'Teifion', 'author_id': 1384652, 'author_profile': 'https://Stackoverflow.com/users/1384652', 'pm_score': 0, 'selected': False, 'text': '<p>I save all my dates as a bigint due to having had issues with the dateTime type before. I save the result of the time() PHP function into it, now they count as being in the same timezone :)</p>\n'}, {'answer_id': 2550233, 'author': 'sbeam', 'author_id': 125875, 'author_profile': 'https://Stackoverflow.com/users/125875', 'pm_score': 2, 'selected': False, 'text': '<p>RE the answer from Željko Živković, timezone descriptors like \'Europe/London\' only work if the mySQL admin has added the timezone tables to the system, and keeps them updated. </p>\n\n<p>Otherwise you are limited to numeric offsets like \'-4:00\'. Fortunately the php date(\'P\') format provides it (as of 5.1.3)</p>\n\n<p>So in say an app config file you might have </p>\n\n<pre><code>define(\'TZ\', \'US/Pacific\');\n....\nif (defined(\'TZ\') &amp;&amp; function_exists(\'date_default_timezone_set\')) {\n    date_default_timezone_set(TZ);\n    $mdb2-&gt;exec("SET SESSION time_zone = " . $mdb2-&gt;quote(date(\'P\')));\n}\n</code></pre>\n\n<p>This means PHP and mySQL will agree on what timezone offset to use.</p>\n\n<p><strong>Always use TIMESTAMP for storing time values</strong>. The column is actually stored as UNIX_TIME (epoch) but implicitly converted from current time_zone offset when written, and back when read.</p>\n\n<p>If you want to display times for users in other time zones, then instead of a global define(), set their given timezone in the above. TIMESTAMP values will be automatically converted by mySQL by the time your app sees the result set (which sometimes can be a problem, if you need to actually know the original timezone of the event too then it needs to be in another column)</p>\n\n<p>and as far as, "why not just store all times as int\'s", that does lose you the ability to compare and validate dates, and means you <em>always</em> have to convert to date representation at the app level (and is hard on the eyes when you are looking at the data directly - quick, what happened at 1254369600?)</p>\n'}, {'answer_id': 42049223, 'author': 'Ravi Tiwari', 'author_id': 1948917, 'author_profile': 'https://Stackoverflow.com/users/1948917', 'pm_score': 0, 'selected': False, 'text': '<p>In php set timezone by in the php.ini file:\n    <code>ini_set("date.timezone", "America/Los_Angeles");</code></p>\n\n<p>or in particular page you can do like:\n    <code>date_default_timezone_set("America/Los_Angeles");</code></p>\n\n<p>In mysql you can do like:\n   <code>SET GLOBAL time_zone = \'America/Los_Angeles\';</code></p>\n'}]
['https://Stackoverflow.com/questions/18449', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2056/']