The standard way for switching from/to user_timezone and gmt

南楼画角 提交于 2019-12-11 23:03:33

问题


I use php's date_default_timezone_set() and set it according to user's timezone field in the the user table. So with ease I can show date/time to users and get date/time from them.

But for storing date related values in database, I have to use the standard GMT timezone.

The question: What is the best or maybe the standard way for switching to/from user_timezone and GMT?

Do you suggest me to follow these steps when I want to insert or update a field in database?

  1. Change the timezone using date_default_timezone_set('GMT');
  2. Get the time using $inserttimestap = time(); or $inserttimestamp = mktime();
  3. Change the timezone back to user default using: date_default_timezone_set($this->session->userdata('timezone'));

回答1:


There is absolutely no need to use date_default_timezone_set('GMT') because for converting to GMT, there is a special set of functions that the function names begin with 'gm':

  • gmmktime() gets a timestamp in GMT timezone.
  • gmdate() that gives a GMT date format.
  • There is also gmstrftime() PHP manual

  • Furthermore it's notable that the time() returns current timestamp only in GMT



来源:https://stackoverflow.com/questions/9099473/the-standard-way-for-switching-from-to-user-timezone-and-gmt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!