对于我们这些使用标准共享托管包(例如 GoDaddy 或 Network Solutions)的人,当您的托管服务器 (PHP) 和 MySQL 服务器位于不同时区时,您如何处理日期时间转换?
另外,对于确定您网站的访问者所在的时区和适本地操作日期时间变量,是否有人提供了一些最佳实践建议?
最佳答案
从 PHP 5.1.0 开始,您可以使用 date_default_timezone_set()函数设置脚本中所有日期/时间函数使用的默认时区。
对于 MySql(引用自 MySQL Server Time Zone Support 页)
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.
您感兴趣的是时区的每个连接设置,您将在脚本开头使用它
SET timezone = 'Europe/London';
至于检测客户端时区设置,您可以使用一些 JavaScript 来获取该信息并将其保存到 cookie 中,并在后续页面读取中使用它来计算正确的时区。
//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);
或者您可以为用户提供自行设置时区的选择。
关于php - 处理不同时区的PHP服务器和MySQL服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18449/