There is now a cross-time zone app where users who log in in different time zones need to see their time zones and also be able to switch time zones.
My idea is that all the storage time in the system is GMT (UTC) time, and when the user logs in, the corresponding display is made according to the time zone in which the user is located.
First look at how the time zone in PHP is set up. The method of setting in PHP is more flexible, you can set the Date.timezone property in PHP.ini, you can call Ini_set (' Date.timezone ', ') by code, or you can use the function date_default_ Timezone_set (), or set in the Htaccess file.
The default time zone for the server, if it does not match the time zone that we want, and we do not have permission to modify the global time zone configuration, only with the help of the code.
PHP also provides a handy function, Gmdate (), which allows us to always get GMT time without worrying about the server's time zone setting, and my idea is based on this function.
My project uses the CodeIgniter framework, and the date helper in the framework provides several handy functions that can be used to handle multi-temporal situations in your application.
where now () always returns the current time of GMT, Local_to_gmt () can convert the local time to GMT, and gmt_to_local () can convert GMT time to local time;
Consider a typical application scenario:
When the user logs in, the current time is displayed. This is where we can use now () to get the standard GMT time, and then use the gmt_to_local () function to convert to the time of the user's time zone.
The user wants to publish a time. Users posted a "2010-07-10 18:30:00" time, we can not directly into the database, we must first use the LOCAL_TO_GMT () conversion standard GMT time into the database, so as to ensure that the entire system time consistency.
The details of these two functions are in fact based on the time zone, and then the corresponding operation is obtained. When calculating, you can also consider daylight saving time, but the start and end times of your time zone will require your own maintenance.
A more complete list of time zones is available in CodeIgniter, Timezone_menu () can display a drop-down list for a time zone, but the time in this list does not correspond exactly to the time zone display in PHP, which is a problem for PHP itself, but can be done by following this function. For each time zone you enter, you can get a corresponding time zone text display.
if (! function_exists ("Tz_offset_to_name")) {/ * Takes a GMT offset (in hours) and returns a timezone name * * Fun Ction tz_offset_to_name ($offset) { $offset *= 3600;//convert hour offset to seconds $abbrarray = timezone_a Bbreviations_list (); foreach ($abbrarray as $abbr) { foreach ($abbr as $city) { if ($city [' offset '] = = $offset) { return $city [' timezone_id ']; }} return FALSE; } }
http://www.bkjia.com/PHPjc/752483.html www.bkjia.com true http://www.bkjia.com/PHPjc/752483.html techarticle There is now a cross-time zone app where users who log in in different time zones need to see their time zones and also be able to switch time zones. My idea is that all the storage in the system ...