How to convert java timestamp to php timestamp?

前端 未结 8 1870
我寻月下人不归
我寻月下人不归 2020-12-20 18:02

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

相关标签:
8条回答
  • 2020-12-20 18:38

    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);
    
    0 讨论(0)
  • 2020-12-20 18:40

    PHP timestamps are seconds, not milliseconds.

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

    That outputs 02 May 2012 22:30:53.

    0 讨论(0)
  • 2020-12-20 18:46

    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.

    0 讨论(0)
  • 2020-12-20 18:52

    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
    
    0 讨论(0)
  • 2020-12-20 18:52

    It's a timestamp in microtime

    Use

    $time = date("whatever you need", $javatime / 1000);
    
    0 讨论(0)
  • 2020-12-20 18:53

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

    0 讨论(0)
提交回复
热议问题