1

How does PHP time() work in following case:

User is posting something in database and posting stamp is added also which is just time().

But how to compensate for time zones? What if two users from EU and USA are posting at the same time, will the posting stamp be the same?

Nadeem Khan
  • 3,408
  • 1
  • 30
  • 41
Munez NS
  • 1,011
  • 1
  • 12
  • 31
  • 2
    [`time()`](http://php.net/time) does ***not*** care about timezones. All it does is return a Unix timestamp (which is the number of seconds since the Unix epoch). See this question for more details: [What is a Unix timestamp and why use it?](http://stackoverflow.com/questions/20822821/). – Amal Murali Apr 20 '14 at 14:26
  • Thx Amal, so what is the good practice in this case? Should I just leave posting stamp as it is from time() function or should I compensate for different time zones(add or subtract hours from posting stamp depending on users country time zone)? – Munez NS Apr 20 '14 at 14:45
  • I'm not sure I understand your question. What are you actually trying to accomplish? – Amal Murali Apr 20 '14 at 14:48
  • I'm confused, because if I have user from USA posting something at 23pm New York Time, posting stamp entered in db will be UTC current time which at that time would be 4am(if Im not wrong). Later on when I try to present date when the record was posted, on web site, I would get 20th August instead of 19th( 19th august 23pm was really the date when record was created) – Munez NS Apr 20 '14 at 15:00
  • 1
    Store the result from time() as is. When showing the time then adjust the displayed time. anything else will end in tears. – Ryan Vincent Apr 20 '14 at 15:18
  • Hehe, this sounds as best option. thx – Munez NS Apr 20 '14 at 15:37

1 Answers1

3

The whole point is that the time()-method stores everything in the same timezone, the one the server is configured with.

If it did not do this, it would be impossible to know when something really happened.

If you want to display datetime with timezones, you can output the timestamp from the server, use javascript or something to calculate the clients timezone offset and add/substract hours to the time.

OptimusCrime
  • 14,662
  • 13
  • 58
  • 96