Implementation method of cross time zone function in PHP application

  • 2021-12-04 09:34:25
  • OfStack

In PHP, a cross-time zone application should be realized, that is, users logged in in different time zones need to see the time of their own time zones, and at the same time, they should be able to switch time zones.

The idea here is that all the stored time in the system is GMT (UTC) time, and when the user logs in, it is displayed according to the time zone where the user is located.

On the use of PHP time function reference: PHP time function use details. Here, we first understand the setting method of time zone in PHP under 1. The setting method in PHP is flexible and varied. You can set date. timezone attribute in php. ini. You can call ini_set(‘date.timezone', ‘') Set, or you can use the function date_default_timezone_set() Or in the htaccess file.

If the default time zone of the server is not set to the time zone we want, and we don't have permission to modify the global time zone configuration, we have to rely on the code.

PHP also provides a convenient function, gmdate() Which allows us to always get the GMT time regardless of the server's time zone setting, and my thinking is based on this function.

My project uses the framework of Codeigniter, and the framework of date, helper, provides several convenient functions that can be used to deal with multiple time zones in applications.

Among them now() The current time of gmt is always returned; local_to_gmt() Local time can be converted into gmt time; gmt_to_local() gmt time can be converted to local time;

Consider a typical application scenario:

After the user logs in, the current time should be displayed. This is what we can use now() Get the standard gmt time, and then use the gmt_to_local() Function to the time in the user's time zone.

The user will publish for 1 time. The user posted a time of "2010-07-10 18: 30:00", which can not be stored directly in the database, so we must use it first local_to_gmt() Convert the standard gmt time into the database, so as to ensure that the time in the whole system remains 1.

The details of these two functions are actually obtained according to the time zone and then the corresponding operations. When calculating, you can also consider daylight saving time, but the start and end times of daylight saving time in your time zone need to be maintained by yourself.

codeigniter provides a relatively complete list of time zones, timezone_menu() You can display a drop-down list of one time zone, but the time in this list cannot completely correspond to the time zone display of PHP, which is the problem of PHP itself. However, you can get a corresponding time zone text display for each input time zone through the following function.


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

Summarize


Related articles: