Answer the question
In order to leave comments, you need to log in
How does timezone work in php and postgres?
Good evening. I got completely confused with time and date, taking into account time zones.
Let's say I have a server in the +3 zone (Moscow).
The web server and database settings are also set to this time zone.
Suppose a user of the system comes from Vladivostok, puts a date in some input relative to his zone and writes the data to the database.
Further, in my view, options are possible depending on what type of data is in the database (with or without timezone). But in neither case, I do not understand how a user different from the time zone of the server will be reflected in his time? After all, he makes a request to the server regarding him, the script works and the time zone of the server is taken into account.
I hope I explained the problem clearly. I would be grateful for clarification)
Answer the question
In order to leave comments, you need to log in
Well, for starters, a user from Vladivostok must somehow inform the server about his zone (for example, by setting it in his profile settings).
Next, PHP must parse the date string (for example, using DateTime::createFromFormat ), and use the zone configured by the user. The resulting date (DateTime object) can be displayed to the user as a date/time in any time zone by doing DateTime::setTimezone() and DateTime::format().
The database works in a similar way: if the TIMESTAMP WITH TIMEZONE type is used, then the time is physically stored in the column in UTC. The client (in this case, the PHP application), when connecting to the database, sets the desired zone (or does not set, then the default zone from the database server settings is used). In this zone, dates are issued to the client. If desired, a field with a timestamp can be transferred to another zone using the datetime_value AT TIME ZONE zone construct .
In theory, the database and PHP use the same zone common to the server, and there is usually no point in changing this. Your task is to correctly parse the dates sent by the user (substituting his preferred zone), as well as display the dates to the user in his zone. The easiest way to set this zone once is via date_default_timezone_set()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question