5

Alright, I can't seem to figure out what is going on, so I have decided to ask you guys. In PHP I am grabbing the UTC timestamp using this code:

date_default_timezone_set("UTC");
time()

This will for example give me 1331065202

Then I have this code in Java to get me the UTC timestamp:

long timestamp = System.currentTimeMillis() / 1000;

This will for example give me 1331093502

Why are the 2 times so different? Shouldn't they both be in UTC Timezone or am I doing something wrong? I am hosted on a VPS and these scrips are on 2 different servers so could it be something on the server side and if so, what can I do?

Marcus Krueger
  • 163
  • 2
  • 11

3 Answers3

5

Given that the two values are wildly different (not even an integer number of hours), I'd say the clock on one of the machines is wrong. (I'm assuming you took the two timestamps at pretty much the same time.)

Those timestamps are:

  • PHP: Tue Mar 06 20:20:02 GMT 2012
  • Java: Wed Mar 07 04:11:42 GMT 2012

Given that it's not March 27th in GMT, it looks like the clock on the Java machine is simply set incorrectly.

If it's a true VPS that you have complete control over, you should look into using NTP or something similar to keep the servers' clocks correct.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Well, the issue there is that this is a plugin for a game. This plugin could be used on many different servers around the world. Would it be possible, and resource efficient to just have Java go to our servers and grab the time? This would be done once every minute per server for possibly several thousand servers. – Marcus Krueger Mar 06 '12 at 20:48
  • @MarcusKrueger: Well if you control all the servers, you can make sure they're all running an NTP sync, can't you? Of course there can still be discrepancies, but they shouldn't be very large. – Jon Skeet Mar 06 '12 at 20:49
  • I edited my comment to include more data. And I would not have control over all servers. – Marcus Krueger Mar 06 '12 at 20:49
  • @MarcusKrueger: If you don't control the servers, then you *certainly* can't assume their time is in sync. You could effectively act as an NTP client from the Java code though, yes - or consult some set of servers you control which would act as time servers themselves (whether through NTP or something else). It's not really very clear what the context is still, I'm afraid... – Jon Skeet Mar 06 '12 at 20:51
  • Alright, I will just query my controlled webserver to grab the current timestamp. I greatly appreciate the detailed and very helpful response! – Marcus Krueger Mar 06 '12 at 20:53
2

As the above people already wrote. use ntp. If your VPS is under your control and it is debian / ubuntu. The following shell script will install it.

    sudo apt-get install ntp

It will start ntp after installing, but if you want to be sure that the daemon are running

    /etc/init.d/ntp restart

Hope that helps.

prdatur
  • 1,010
  • 1
  • 14
  • 20
0

these scrips are on 2 different servers

There's a clue, right there: your two servers have different times set.

If you need your Java and PHP apps to be in synch, consider having both servers use the Network Time Protocol.

Ben
  • 7,548
  • 31
  • 45