I examined this situation in detail in the PHP / MySQL application that I wrote for a private jet operator just over a year ago. There are various time zone processing strategies on these two platforms, but I will explain how I did it. I installed the MySQL server in UTC and ran each PHP script in the time zone that the user indicates during the registration process for the user profile.
MySQL and PHP (PHP 5.2 and later) have their own datetime data types. MySQL datetime is a primitive data type, and PHP 5.2 and later offers a built-in DateTime class . MySQL's datetime data type does not include metadata for the time zone, but the PHP DateTime object always includes the time zone. If the PHP datetime constructor does not specify an optional time zone in the second argument, then the PHP datetime constructor uses the php environment variable.
Both MySQL and PHP have a default time zone in the configuration files. MySQL uses the datetime set in the configuration file for each db connection, unless the user sets a different time zone after the connection is started using the SET time_zone = [timezone]; . PHP also sets the time zone environment variable for each script using the time zone specified in the server configuration file, and this environment variable can be overridden using the date_default_timezone_set() PHP function. after running the script.
The PHP DateTime class has a timezone property, which is a PHP DateTimeZone object. The DateTimeZone object is specified using a string for the exact time zone. The time zone list is comprehensive, with hundreds of individual time zones around the world. PHP time zones will automatically consider daylight saving time.
When a user creates a date-time in a web application, create a PHP datetime object in the time zone of the user profile. Then use the setTimezone method to change the DateTime object to the UTC time zone. Now you have the user datetime in UTC, and you can save the value in the database. Use the DateTime format method to express the data as a string in the format accepted by MySQL.
Thus, the user generates a date-time, and you create the PHP datetime object in the user-specified time zone:
// 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('Ymd H:i:s'); $query_string="INSERT INTO `t_table1` (`datetime1`) VALUES('$formated_string')";
When you retrieve a value from the database, create in UTC and then convert it 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 db, and the user always experiences the values ββin the time zone of his profile. PHP will adjust daylight saving time if necessary for each time zone.
One of them: this explanation does not apply to the MySQL timestamp data type. I recommend using the MySQL datetime datatype to store date and time values, not the timestamp datatype. The timestamp data type is given in the manual here .
Edit: You can create an array containing each line in the PHP time range using listIdentifiers , which is a static method of the DateTimeZone class.