How to convert java timestamp to php timestamp?

2019-01-27 21:58发布

问题:

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!

回答1:

PHP timestamps are seconds, not milliseconds.

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

That outputs 02 May 2012 22:30:53.



回答2:

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);


回答3:

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


回答4:

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

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


回答5:

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);


回答6:

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.



回答7:

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



回答8:

It's a timestamp in microtime

Use

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