#
#
# artur #2015-04-19 16:47:54
PHP
# artur #, 2015-04-19 16:47:54

How to use php time() correctly?

Hello!
I got confused completely with the storage of the time of user actions.
Initially, everything was built in this way: the user enters the site, logs in, the script determines his IP and makes a request to the geo-ip service, which returns the time zone, let's say Europe/Moscow , save it to the database ... (this is the case with each authorization)
Basically I set the controller: habrahabr.ru/post/124297 And then problems and confusion begin, for example, the user writes a message, the script saves it to the database, enters the data received from time() into the field with the time of the record. It turns out the result of time() will be built based on the user's time zone - Europe/Moscow, not the server and not UTC...
date_default_timezone_set($this->user->timezone);



How to be? It seems to me with logic that I initially pierced specifically. Now I see only one way out, to shovel a ton of code (models with entering information into the database) and insert the line there: ... although I still feel that something is wrong here. How to do it right and conveniently? it was probably a mistake to store time() in an INT(10) field...
date_default_timezone_set("UTC");

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2015-04-19
@By_Engine

For the site, use a single zone. For example London (utc 0). And for a specific user to shift the time

M
Mikhail Osher, 2015-04-19
@miraage

date_default_timezone has no effect on time().
It affects date(), strftime().

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question