Mohammad Naji
Mohammad Naji

Reputation: 5442

The standard way for switching from/to user_timezone and gmt

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'));

Upvotes: 0

Views: 183

Answers (1)

Mohammad Naji
Mohammad Naji

Reputation: 5442

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

Upvotes: 1

Related Questions