In PHP, you need to implement a cross-time zone application, that is, users who log in to different time zones need to see their time zone time, and you must be able to switch time zones.
The idea here is that all stored times in the system are GMT (UTC) time. When the user logs in, the corresponding display will be performed according to the time zone where the user is located.
For reference, please refer to the use of time functions in PHP:Detailed explanation of PHP time function usage. Here we first understand the method of setting time zones in PHP. The methods for setting in PHP are relatively flexible and diverse. You can set properties in it, and you can use code to call it.ini_set(‘', ‘')
Settings, you can also use functionsdate_default_timezone_set()
, or set in the htaccess file.
If the default time zone of the server does not match the time zone we want, and we do not have permission to modify the global time zone configuration, we can only rely on the code.
PHP also provides a convenient function,gmdate()
, which allows us to always get GMT time without caring about the server's time zone settings. My idea is based on this function.
I use Codeigniter framework in my project. The date helper in the framework provides several convenient functions that can be used to handle multi-time zone situations in the application.
innow()
The current time of gmt is always returned;local_to_gmt()
The local time can be converted to 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 must be displayed. This is what we can usenow()
Obtain the standard gmt time and usegmt_to_local()
The time when the function is converted into the user's time zone.
The user needs to publish for a time. The user posted a time of "2010-07-10 18:30:00". We cannot directly deposit it into the database, we must first use it.local_to_gmt()
The conversion standard gmt time is stored in the database, so as to ensure that the time in the entire system remains consistent.
The details of these two functions are actually calculated based on the time zone and then performed corresponding operations. When calculating, you can also consider daylight saving time, but the start and end time of the daylight saving time in the time zone need to be maintained by yourself.
A relatively complete list of time zones is provided in codeigniter.timezone_menu()
A drop-down list of time zones can be displayed, but the time in this list cannot fully correspond to the time zone display provided by PHP. This is a problem with PHP itself. However, the following function can be used to enable each time zone input to obtain a corresponding time zone text display.
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
The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the following links