Home >Backend Development >PHP Tutorial >PHP millisecond timestamp calculation method, how to obtain millisecond timestamp in PHP
An example of how to obtain millisecond timestamps in PHP. The time function in PHP programming can obtain timestamps. This article compares the different writing methods and efficiencies of how to obtain millisecond timestamps in PHP. For those who are interested, please refer to it.
php millisecond timestamp calculation method The timestamp obtained by the time() function in php, the unit is seconds. The timestamp obtained by the front-end js, the unit is milliseconds. How to unify the timestamps of JS and PHP, that is, how to get the millisecond timestamp using PHP? Example: Copy code Code example: "; echo getMillisecond_new().' PHP millisecond-getMillisecond_new()'; /* * Idea: * 1. Use microtime() to obtain the microsecond timestamp, format: 0.69718900 1420440552 * 2. Add the two parts before and after × 1000, then round to round($float,0) * Seconds time()-->milliseconds-->microseconds microtime(), the interval between two is 1000 decimal * In this way, it can be consistent with the timestamp of the front-end JS * JS: new Date().getTime() gets the millisecond timestamp */ ?><script> var time=new Date(); var mtime=time.getTime(); document.write(mtime+' JS获得毫秒时间戳'); </script> Run results: 1424069168633 PHP milliseconds-getMillisecond() 1424069168633 PHP millisecond-getMillisecond_new() 1424069168643 JS gets millisecond timestamp It can be seen that the third timestamp value is slightly larger than the first two. This is the time consumed by the code running, which is normal. |