Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Timestamp and PHP Timestamp giving 2 different times

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?

like image 877
Marcus Krueger Avatar asked Mar 06 '12 20:03

Marcus Krueger


1 Answers

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.

like image 144
Jon Skeet Avatar answered Sep 21 '22 01:09

Jon Skeet