How to convert java timestamp to timestamp php?

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

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

Help pls !!! I am looking for more than 1.5 hours for soloution!

+6
source share
8 answers

PHP timestamps are seconds, not milliseconds.

 echo gmdate("d MYH:i:s",1335997853); 

This displays 02 May 2012 22:30:53 .

+7
source

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

Which php supports too , with the exception of seconds, so the following should work in php:

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

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

  print(date("r", 1335997853142/1000) 
+1
source

Do you have a timestamp in milliseconds, when it should be in seconds

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

Exit

 02 May 2012 22:30:53 
+1
source

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

Java Timestamp: 1396959462222

PHP Timestamp: 1396959462

Dividing by 1000 does not give the correct answer, because it will give a double or floating value, i.e. 1396959462.222 what we do not want. We need an integer, for example 1396959462. Thus, the correct way to convert a Java timestamp to a PHP timestamp can be used using the intval () method:

 $php_timestamp = intval($java_timestamp/1000); 

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

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

date needs a timestamp in integer format. So I think U should delete the last 3 numbers. Do not use division because it will return a floating point number.

+1
source

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

0
source

This is a timestamp in microtime

Using

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

Source: https://habr.com/ru/post/914852/


All Articles