2

I have to maintain some old browsergame and in this game the user has planets which produce ressources. When an user opens the planet view, the amount of ressources is calculated by building the the diffence between the last update of the planet and the current time, like this:

$secondsBetweenUpdate = time() - $lastUpdate; // Timestamps

This night there was time shift (in at least Germany from daylight saving time to normal time - or as we say in German from "summer time" to "winter time") and so between the "new" 2AM and "new" 3AM the above difference is negative and so the player looses ressources instead of getting them.

For sure I can avoid the loosing by setting $secondsBetweenUpdate to zero if it is getting negative but is there a way to handle the shift correctly? To be honest I'm not even sure what would be right - regardless the change from daylight saving time to normal time in autum or the change from normal time to daylight saving time.

Can anyone give me some advise to handle such time shifts?

I saw this SO Thread about Daylight saving time and time zone best practices and one advise is to store all times as Zulu time. How would I do that and how can I show the time for a user correctly - meaning the time he sees is the same as on his e.g. watch?

We use PHP (with of course CSS and JavaScript), Mysql and are partly migrated to cakePHP framework.

Community
  • 1
  • 1
bish
  • 3,381
  • 9
  • 48
  • 69

1 Answers1

3

Use the GMT timezone on your server, which doesn't have time shifts.

The manner of doing this varies system-by-system. In Ubuntu, for example, you would do the following:

sudo dpkg-reconfigure tzdata

And select the GMT option.

Then, if you need to display dates, you can use PHP's date library to change timezone to suit your client's preferences, e.g. (for Eastern Standard Time):

$date = new DateTime($lastUpdate);
$date->setTimezone(new DateTimeZone('EST'));

PHP will handle time shifts, e.g. due to DST in the US

Steven Moseley
  • 15,871
  • 4
  • 39
  • 50