M
M
makar042019-02-10 00:13:25
PHP
makar04, 2019-02-10 00:13:25

How to account for time difference when working with timezones in php and sql?

Good day! I work with a reporting system. I ran into a problem when fetching data from the database taking into account time zones.
The server works according to Moscow time.
When writing data to the database, the client can set the date and time of the recording, the record is inserted not the one that the client specified, but is transferred to the Moscow time zone, and the client is already displayed in his time zone.
For example:
The client made an entry on 2019-02-10 at 00:01:55 Ekaterinburg time UTC+5, and 2019-02-09 22:01:55 Moscow UTC+3 was recorded in the database, then the client wants to receive all records for 2019 -02-10 and makes sql query

"SELECT * FROM parcel WHERE DATE(date_added) >= '2019-02-10' AND DATE(date_added) <= '2019-02-10'";

Accordingly, since we have a record date of 2019-02-09 22:01:55, it will not get the desired result.
I thought about inserting the real date and time, that is, not according to the server time, but according to the time that the client has, but since records can be worked from different accounts and from different time zones, I realized that this is not an option.
How can you get out of the situation?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bkosun, 2019-02-10
@makar04

Obviously, before making a fetch, you need to convert the requested date to the system time zone, as you do when writing to the database.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question