问题
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?
- Change the timezone using
date_default_timezone_set('GMT'); - Get the time using
$inserttimestap = time();or$inserttimestamp = mktime(); - 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 manualFurthermore 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