Oliver Dixon
Oliver Dixon

Reputation: 7405

Comparing a PHP timestamp(int) to a java timestamp(long)

I've hit a brick wall.

I'm downloading encrypted level data from a server (made with a level creator I've created).

The level data stores the last updated version of the data with a long using Java.

Now to update levels, I download a list of available levels with the time they were last modified on file using php.

Many would say I'd have to store the time somewhere else, like json. Tis not an option.

I get the following results:

Fri May 08 23:05:24 CEST 2015 | timeStampJava: 1431119124273
Sat Jan 17 14:31:58 CET 1970 | fileTimeStampPHP(filemtime): 1431118989
OLLY:LOG: ---
Fri May 08 23:05:28 CEST 2015 | timeStampJava: 1431119128871
Sat Jan 17 14:31:58 CET 1970 | fileTimeStampPHP(filemtime): 1431118989
OLLY:LOG: ---
Fri May 08 23:05:32 CEST 2015 | timeStampJava: 1431119132288
Sat Jan 17 14:31:58 CET 1970 | fileTimeStampPHP(filemtime): 1431118989
OLLY:LOG: ---
Fri May 08 23:05:35 CEST 2015 | timeStampJava: 1431119135289
Sat Jan 17 14:31:58 CET 1970 | fileTimeStampPHP(filemtime): 1431118989
OLLY:LOG: ---
Fri May 08 23:05:38 CEST 2015 | timeStampJava: 1431119138807
Sat Jan 17 14:31:58 CET 1970 | fileTimeStampPHP(filemtime): 1431118989

I'm trying to compare the two using:

if(serverLevelInfo.last_updated > localLevelStorage.getLastUpdated())

Problem is if I downcast i.e

phpTimeStamp > (int)javaTimeStamp

It produces totally inaccurate results.

What does one do now?

Edit 1

Attempted suggestion:

//TODO Problem PHP int timestamp to java long timestamp.
                            PolyUtils.log((long)(serverLevelInfo.last_updated * 1000));
                            PolyUtils.log(localLevelStorage.getLastUpdated());
                            PolyUtils.log("---");

Result (wrong)

OLLY:LOG: 894879432
OLLY:LOG: 1431119124273
OLLY:LOG: ---
OLLY:LOG: 894879432
OLLY:LOG: 1431119128871
OLLY:LOG: ---
OLLY:LOG: 896347432
OLLY:LOG: 1431119132288
OLLY:LOG: ---
OLLY:LOG: 894879432
OLLY:LOG: 1431119135289
OLLY:LOG: ---
OLLY:LOG: 894879432
OLLY:LOG: 1431119138807
OLLY:LOG: ---

Upvotes: 3

Views: 596

Answers (2)

Oliver Dixon
Oliver Dixon

Reputation: 7405

Unfortunately the only solution is that you have to lose some accuracy on the JAVA long. This is because PHP cannot produce longs (32bit scalar vars.)

if(serverLevelInfo.last_updated > (int)(localLevelStorage.getLastUpdated() / 1000))

Make sure that you cast int AFTER you've taken length off the long otherwise you'll be inaccuracies.

Upvotes: 1

Tadeáš Jílek
Tadeáš Jílek

Reputation: 2833

You need to multiply unix timestamp (php) by 1000, because java expects miliseconds.

java.util.Date time=new java.util.Date((long)timeStamp*1000);

Upvotes: 0

Related Questions