在PHP Web应用程序中使用时区 [英] Using time zones in a PHP web application

查看:95
本文介绍了在PHP Web应用程序中使用时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在一直在寻找几个小时,了解在PHP / MySQL Web应用程序中使用时区的最佳方式,找到明确的答案很困难。从我迄今为止所了解到的情况来看,最好将每个人的东西都存储在UTC的数据库中(如果我错了,请纠正我)。

当用户注册时,我会问他们那里的时区,然后存储在那里用户。这将作为下拉菜单的格式显示:

 < option value =Europe / London>(GMT)格林尼治标准时间:伦敦< / option> 

我正在构建的应用程序将允许用户在将来与人员(会议)像日历一样。很显然,在一年的时间里,不同的时区有不同的夏令时期,任何想法如何满足这个要求? 来自英国的用户设定了3次会议:2013年1月24日00PM,并邀请住在加利福尼亚州的人参加这次会议,我如何获得该信息以便美国人在他/她的时区看到该会议,而英国用户在他/她的时区看到该会议? (请注意,这两个用户都已注册并设置了他们的时区)。



有没有人有明确的解释,也许有一些例子呢?或者可以指点我在哪里可以找到它?

谢谢

解决方案

<我在一年多前为一家私人飞机运营商编写的PHP / MySQL应用程序中广泛处理了这种情况。在这两个平台上处理时区有不同的策略,但我会解释我是如何做到的。我将MySQL服务器设置为UTC,并在用户配置文件的注册过程中为用户指定的时区中运行每个PHP脚本。

MySQL和PHP(PHP 5.2和上面)都有本地日期时间数据类型。 MySQL的日期时间是一种原始数据类型,而PHP 5.2及以上版本则提供内置的 DateTime类 。 MySQL datetime数据类型不包含时区的元数据,但PHP DateTime对象始终包含时区。如果PHP datetime构造函数没有在第二个参数中指定可选的时区,那么PHP datetime构造函数使用php环境变量。

MySQL和PHP都在配置文件中设置了默认时区。 MySQL使用每个数据库连接的在配置文件中设置的日期时间,除非用户在使用命令 SET time_zone = [timezone]; 开始连接后指定不同的时区。 PHP还使用服务器配置文件中设置的时区为每个脚本设置时区环境变量,并且可以使用PHP函数

$ date_default_timezone_set() 。 > PHP DateTime类有一个名为 timezone 的属性,它是一个 PHP DateTimeZone对象一>。 DateTimeZone对象是使用确切时区的字符串指定的。 时区列表非常全面,在全球拥有数百个独立时区。 PHP时区会自动计算夏令时。 当用户在Web应用程序中生成日期时间时,构建一个PHP日期时间对象在用户配置文件的时区中。然后使用 setTimezone 方法修改将DateTime对象设置为UTC时区。现在,您可以使用UTC的用户日期时间,并且可以将该值存储在数据库中。使用DateTime 格式 方法来表达数据作为MySQL接受格式的字符串。



因此,用户生成一个日期时间,并且在用户指定的时区中创建一个PHP日期时间对象:

  //使用用户配置文件的包含文件设置
$ user_timezone = new DateTimeZone('America / New_York');

// PHP接受格式的1st参数strtotime
$ date_object1 = new DateTime('2012/8/9 5:19',$ user_timezone);
$ date_object1-> setTimezone(new DateTimeZone('UTC'));
$ formated_string = $ date_object1->格式('Y-m-d H:i:s');
$ query_string =INSERT INTO`t_table1`(`datetime1`)VALUES('$ formated_string');

从数据库中检索值时,以UTC构造,然后转换为用户的时区。

  $ query_string =SELECT`datetime1` FROM`t_table1`; 
$ date_object1 = new DateTime($ datetime_string_from_mysql,new DateTimeZone('UTC');
$ date_object1-> setTimezone($ user_timezone);
$ string_for_display_in_application = $ date_object1->格式( 'm / d / Y g:i');

使用此方法,您的日期时间值为总是存储在数据库中的UTC,并且用户总是体验他/她的个人资料时区中的值PHP如果必要的话会针对每个时区修正夏令时。



<一个问题:这个解释不包括MySQL时间戳数据类型,我推荐使用MySQL datetime datetype来存储日期时间值,而不是timestamp数据类型。时间戳数据类型在手册此处



编辑:
您可以使用 listIdentifiers ,它是DateTimeZone类的静态方法。


I've been looking around for a few hours now about what's the best way to use timezones in a PHP/MySQL web application, finding a definitive answer is hard. From what I've learnt so far it is best to store everyones stuff in the database in UTC (correct me if I am wrong).

When a user registers I will ask them for there timezone and then store that against there user. This would be in this format as a dropdown menu:

<option value="Europe/London">(GMT) Greenwich Mean Time : London</option>

The app I am building will allow users to set arrangements in the future with people (meetings), much like a calendar. Obviously over the course of a year different timezones have different daylight savings periods, any idea how I would cater for this?

Say a user from the UK sets a meeting for 3:00PM on January 24th 2013 and invites someone who lives in California to this meeting, how do I get it so that the American sees that meeting in his/her timezone and the UK user sees it in his/her timezone? (Note that both users are signed up and have set their timezone).

Does anyone have a clear explanation and maybe some examples for this? Or can point me to where I can find that?

Thanks

解决方案

I dealt with this situation extensively in a PHP/MySQL application I wrote for a private jet operator a little over a year ago. There are different strategies to handle timezones in these two platforms, but I will explain how I did it. I set the MySQL server to UTC and run each PHP script in the timezone that the user specifies during the signup process for the user profile.

MySQL and PHP (PHP 5.2 and above) both have native datetime datatypes. MySQL's datetime is a primitive data type, while PHP 5.2 and above offers the built-in DateTime class. The MySQL datetime datatype does not include metadata for the timezone, but a PHP DateTime object always includes a timezone. If the PHP datetime constructor does not specify the optional timezone in the second argument, then the PHP datetime constructor uses the php environment variable.

Both MySQL and PHP have default timezone set in the configuration files. MySQL uses the datetime set in the config file for each db connection unless the user specifies a different timezone after connection is started with the command SET time_zone = [timezone];. PHP also sets a timezone environment variable for each script using the timezone set in the server config file, and this environment variable can be overriden using the PHP function date_default_timezone_set() after the script starts.

The PHP DateTime class has a property called timezone, which is a PHP DateTimeZone object. The DateTimeZone object is specified using a string for the exact time zone. The list of timezones is comprehensive, having hundreds of individual time zones across the world. The PHP time zones will account for daylight savings time automatically.

When the user generates a datetime in the web app, construct a PHP datetime object in the timezone of the user's profile. Then use the setTimezone method to modify the DateTime object to the UTC timezone. Now you have the user's datetime in UTC, and you can store the value in the database. Use the DateTime format method to express the data as a string in the format accepted by MySQL.

So the user generates a datetime, and you make a PHP datetime object in the user's specified timezone:

// set using an include file for user profile
$user_timezone = new DateTimeZone('America/New_York'); 

// 1st arg in format accepted by PHP strtotime
$date_object1 = new DateTime('8/9/2012 5:19 PM', $user_timezone); 
$date_object1->setTimezone(new DateTimeZone('UTC'));
$formated_string = $date_object1->format('Y-m-d H:i:s');
$query_string="INSERT INTO `t_table1` (`datetime1`) VALUES('$formated_string')";

When you retrieve the value from the database, construct in UTC and then convert to the user's time zone.

$query_string="SELECT `datetime1` FROM `t_table1`";
$date_object1=new DateTime($datetime_string_from_mysql, new DateTimeZone('UTC');
$date_object1->setTimezone($user_timezone);
$string_for_display_in_application = $date_object1->format('m/d/Y g:i a');

Using this method, your datetime values are always stored in UTC inside the db, and the user always experiences the values in his/her profile's time zone. PHP will correct for Daylight Savings Time if necessary for each time zone.

One gotcha: This explanation does not cover the MySQL timestamp datatype. I recommend using the MySQL datetime datetype to store datetime values, not the timestamp datatype. The timestamp datatype is covered in the manual here.

Edit: You can produce an array containing every PHP timezone string using listIdentifiers, which is a static method of the DateTimeZone class.

这篇关于在PHP Web应用程序中使用时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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