Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The main problem is that MySQL's support for timezones is <em>laughable</em>. Most other databases let you define the timezone as part of the datetime column, but not MySQL. Instead, they force you to do a connection-wide or server-wide timezone setting.</p> <p>Your best bet is making sure MySQL stores all datetimes as GMT / UTC. You can enforce this by running <code>SET time_zone = 'UTC'</code> on connect. Do it at the same time as you set the character set. You <em>are</em> setting the connection character set, <em>right</em>?</p> <p>PHP-side, you can then use a combination of <a href="http://php.net/class.datetime" rel="nofollow">DateTime</a> and <a href="http://php.net/class.datetimezone" rel="nofollow">DateTimeZone</a> to take the datetimes from MySQL and display them in the user's timezone. For example, let's pretend that we get the date <code>2012-11-13 14:15:16</code> from a MySQL <code>DATETIME</code> column. From the PHP interactive prompt:</p> <pre><code>php &gt; $from_mysql = '2012-11-13 14:15:16'; php &gt; $utc = new DateTimeZone('UTC'); php &gt; $ts = new DateTime($from_mysql, $utc); php &gt; $pdt = new DateTimeZone('America/Los_Angeles'); php &gt; $ts_pdt = clone $ts; php &gt; $ts_pdt-&gt;setTimezone($pdt); php &gt; echo $ts_pdt-&gt;format('r'), "\n"; Tue, 13 Nov 2012 06:15:16 -0800 </code></pre> <p>As demonstrated, you just need to create the DateTime by expressly telling it you're UTC, if UTC isn't the timezone you've set using <code>date_default_timezone_set</code>. Switching the timezone of a DateTime is as easy as giving it a new one. I've used <code>clone</code> here to work on a copy. DateTimes are mutable, and it's sometimes easy to find yourself accidentally clobbering it.</p> <p>Reverse date math works the same way, just transform their selection into UTC and run the math on the native numbers.</p> <p>tl;dr:</p> <ul> <li><em>Store</em> and <em>perform calculations on</em> datetimes in UTC (GMT) only</li> <li><em>Display</em> all datetimes in the user's timezone only</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload