8

how can I convert a Java timestamp like this one 1335997853142 to a format that php can read? 1335997853142 should be 02 May 2012 22:30:53.

But what ever I try I get errors or PHP says 1970-01-01 01:00:00 or 2038-01-19 04:14:07

PLS help!!! I'm searching for over 1.5h for a soloution!

3
  • 1
    Use java.util.Calendar. That's all you get for this great question. Commented May 3, 2012 at 22:27
  • 1
    Java timestamp is milliseconds from linux time, php is seconds from linux time. Divide java time by 1000 to get php time. Commented May 3, 2012 at 22:28
  • @Torious: dont want to use java - i'm converting 1220 values from a xml file with php to mysql Commented May 3, 2012 at 22:31

8 Answers 8

8

PHP timestamps are seconds, not milliseconds.

echo gmdate("d M Y H:i:s",1335997853);

That outputs 02 May 2012 22:30:53.

Sign up to request clarification or add additional context in comments.

1 Comment

damn it!! ok, now works - with all of your soloutions! thx!! problem was: the string has spaces (dont know why!). after a trim() everything worked. sorry guys but thank you all!
6

it is not a java timestamp, it is milliseconds since epoch (1970-01-01 00:00:00 GMT)

Which php supports too except in seconds so the following should work in php:

date('choose your format', javaMilliseconds / 1000);

1 Comment

as Mark Reed says also date_default_timezone_set("UTC");
2

Date/Time: April / 08 / 2014 12:17:42pm UTC

Java timestamp: 1396959462222

PHP timestamp: 1396959462

Dividing by 1000 doesn't give exactly the right answer because it will give a double or float value, i.e. 1396959462.222 which is not what we want. We need an integer here like 1396959462. So correct way to convert Java timestamp to PHP timestamp would be by using intval():

$php_timestamp = intval($java_timestamp/1000);

In a real example, in one of my Android apps where I send Java timestamp to a PHP server, I do it as mentioned above. Also, for good security practice, I add a preg_replace() to make sure if someone added hack code in this field, it is removed:

$php_timestamp = intval(preg_replace('/[^0-9]/', '', $java_timestamp)/1000);

Comments

1

The timestamp you have is in milliseconds when it should be in seconds

$java_time = 1335997853142;
$php_time = $java_time / 1000;
$today = date("d M Y G:i:s", $php_time);

Output

02 May 2012 22:30:53

3 Comments

this gives for all values in the array: 25 Jan 1970 20:31:23
I was missing some semicolons, fixed now
Are you sure you are retrieving the entries from xml properly?
1

Java gives you a timestamp in milliseconds. PHP uses Unix seconds, so divide by 1000:

  print(date("r", 1335997853142/1000)

Comments

1

date need a timestamp in integer format. So I think U should remove 3 last number. Not use division beause it will return float number.

Comments

0

Use the date() function in php, the parameters are found here: http://php.net/manual/en/function.date.php

Comments

0

It's a timestamp in microtime

Use

$time = date("whatever you need", $javatime / 1000);

1 Comment

i used this: date('Y-m-d H:i:s', $values['date'] / 1000); // 1970-01-25 21:31:23 this: date('Y-m-d H:i:s', (int) $values['date'] / 1000) // 1970-01-25 21:31:23 this: date('U = Y-m-d H:i:s', (int) $values['date']) // 2038-01-19 04:14:07 this: date('U = Y-m-d H:i:s', (int) $values['date']) // Warning: date() expects parameter 2 to be long wtf is wrong?!?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.