PHP applications across time zones tips implementation _php function - PHP

Source: Hi learning network sensitive and eager Forum www.piaodoo.com welcome to learn from each other

PHP in order to achieve a cross-application time zone, which is the user's login time zones need to see their own time zone time, but also be able to switch time zones.

Here the idea is that all the stored system time is GMT (UTC) time, when the user logs in, a corresponding display area according to the user's time.

About time using PHP function to reference: PHP time function uses detailed. Here we first look for setting up PHP in time zones. The method of setting the comparison PHP flexible, may be provided in php.ini date.timezone properties, through the code, call ini_set(‘date.timezone', ‘')set, the function may be used date_default_timezone_set()or disposed htaccess file.

The default time zone server, and if we want to set the time zone does not match, and we do not have permission to modify the global time zone configuration, only by means of a code.

PHP also provides a convenient function gmdate()that lets us do not care about the server's time zone settings and always get the GMT time, my thinking is based on this function.

In this framework I used Codeigniter project, in the framework of the date of this helper function provides several convenient and can be used multiple time zones case processing applications.

Which now() always returns gmt current time; local_to_gmt() you can convert local time gmt time; gmt_to_local() you can convert gmt time is local time;

Consider a typical scenario:

After user login, to display the current time. This is what we can use now()to get gmt standard time, and then use the gmt_to_local()function into a user's time zone.

To publish a user time. Users issued a "2010-07-10 18:30:00" of the time, we can not directly stored in the database, you must use local_to_gmt() the conversion gmt standard time stored in the database, so as to ensure that time the entire system is consistent.

Details of these two functions, in fact, are based on the time zone, then the corresponding operation come. When the calculation of the time, you can also consider daylight saving time, but the time zone daylight saving time start and end time, you will need to maintain their own.

codeigniter provides a more complete list of time zones, timezone_menu() you can display the drop-down list of time zones, but the time this list does not correspond exactly to the PHP's own time zone, this is a problem PHP itself, but by the following function to allow the input of each time zone, you can get a corresponding time zone text.

if( ! function_exists("tz_offset_to_name") ) 
{ 
  /* Takes a GMT offset (in hours) and returns a timezone name */ 
  function tz_offset_to_name($offset) 
  { 
      $offset *= 3600; // convert hour offset to seconds 
      $abbrarray = timezone_abbreviations_list(); 
      foreach ($abbrarray as $abbr) 
      { 
          foreach ($abbr as $city) 
          { 
              if ($city['offset'] == $offset) 
              { 
                  return $city['timezone_id']; 
              } 
          } 
      } 
      return FALSE; 
  } 
}

to sum up

That's all for this article, I hope the contents of this paper has some reference value of learning for all of us to learn or work, thank you for the support sensitive and eager Forum / Hi learning network. If you want to know more details, please see the related links below

The original address is: http: //www.piaodoo.com/thread-3547-1-1.html

Guess you like

Origin www.cnblogs.com/txdah/p/12093310.html
php