Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's what we did with our servers. We set everything to use UTC, and we display in the user's time zone by converting from UTC on the fly. The code at the bottom of this post is an example of how to get this to work; you should confirm that it works in all cases with your setup (i.e. daylight savings, etc).</p> <h2>Configuring CentOS</h2> <ol> <li>Edit <code>/etc/sysconfig/clock</code> and set <code>ZONE</code> to <code>UTC</code></li> <li><code>ln -sf /usr/share/zoneinfo/UTC /etc/localtime</code></li> </ol> <h2>Configuring MySQL</h2> <ol> <li><p>Import timezones into MySQL if necessary:</p> <p><code>mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql</code></p></li> <li><p>Edit my.cnf and add the following within the [mysqld] section:</p> <p><code>default-time-zone = 'UTC'</code></p></li> </ol> <h2>PHP Code</h2> <pre><code>&lt;?php /* Example usage: $unixtime = TimeUtil::dateTimeToTimestamp('2009-04-01 15:36:13'); echo TimeUtil::UTCToPST("M d, Y - H:i:s", $unixtime); */ // You should move this to your regular init method date_default_timezone_set('UTC'); // make this match the server timezone class TimeUtil { public static function timestampToDateTime($timestamp) { return gmdate('Y-m-d H:i:s', $timestamp); } public static function dateTimeToTimestamp($dateTime) { // dateTimeToTimestamp expects MySQL format // If it gets a fully numeric value, we'll assume it's a timestamp // You can comment out this if block if you don't want this behavior if(is_numeric($dateTime)) { // You should probably log an error here return $dateTime; } $date = new DateTime($dateTime); $ret = $date-&gt;format('U'); return ($ret &lt; 0 ? 0 : $ret); } public static function UTCToPST($format, $time) { $dst = intval(date("I", $time)); $tzOffset = intval(date('Z', time())); return date($format, $time + $tzOffset - 28800 + $dst * 3600); } } </code></pre>
 

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