Marcus Krueger
Marcus Krueger

Reputation: 163

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?

Upvotes: 5

Views: 513

Answers (3)

prdatur
prdatur

Reputation: 1009

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.

Upvotes: 2

Ben
Ben

Reputation: 7597

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.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500485

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.

Upvotes: 5

Related Questions