Using PHP to Match a Javascript Timestamp

后端 未结 3 1069
面向向阳花
面向向阳花 2021-01-23 04:25

Alright when I do the code:


You get the Unix t

相关标签:
3条回答
  • 2021-01-23 04:56

    If you don't need (millisecond) presision, then just divide & Math.floor the javascript's function. So:

    time();
    

    and

    Math.floor((new Date).getTime()/1000)
    

    should return the same value at the same time.

    0 讨论(0)
  • 2021-01-23 04:56

    The javascript

    getTime() ;
    

    method returns the number of milliseconds since midnight of January 1, 1970 and the specified date.

    A php equivalent is

    time() * 1000; // not microtime() as I wrongly said earlier.
    

    However they wont match as php does not support millisecond precision it seems.

    0 讨论(0)
  • 2021-01-23 05:05

    What you are looking for is millisecond time in PHP. To accomplish this you need to use a combination of the microtime function and some multiplication.

    microtime when passed true as its first parameter will return the current time as the number of seconds since the Unix epoch to the nearest microsecond.

    To convert the value into an integer value of the number of milliseconds since the Unix epoch you must multiply this value by 1000 and cast to an integer.

    So:

    $milliseconds = (int)(microtime(true) * 1000);
    
    0 讨论(0)
提交回复
热议问题