在不同的时区处理PHP服务器和MySQL服务器 [英] Dealing with PHP server and MySQL server in different time zones

查看:161
本文介绍了在不同的时区处理PHP服务器和MySQL服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于那些使用标准共享托管软件包(如GoDaddy或Network Solutions)的用户,当托管服务器(PHP)和MySQL服务器位于不同的时区时,您如何处理日期时间转换?

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?

另外,有没有人有一些最佳实践建议来确定你的站点访问者的时区是否适当地处理datetime变量?

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?

推荐答案

从PHP 5.1.0起,您可以使用 date_default_timezone_set() 功能设置脚本中所有日期/时间函数使用的默认时区。

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.

对于MySql(引自 MySQL服务器时区支持页面)

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


在MySQL 4.1.3之前,服务器仅运行在启动时设置的系统时区。从MySQL 4.1.3开始,服务器维护了几个时区设置,其中一些可以在运行时进行修改。

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';

对于检测客户端时区设置,您可以使用一些JavaScript来获取并保存该信息到一个cookie,并在随后的页面读取中使用它来计算正确的时区。

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);

或者您可以向用户提供chioce自己设置他们的时区。

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

这篇关于在不同的时区处理PHP服务器和MySQL服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆