This article mainly introduces the method of getting the millisecond timestamp of PHP, which involves the processing technique of PHP to return the result of microtime function, it is very simple and practical, the friend who needs can refer to the following
The example in this paper describes how PHP gets the millisecond timestamp. Share to everyone for your reference. The specific analysis is as follows:
PHP itself does not provide a function to get a millisecond timestamp, which can be obtained by gettime () in Java. If you are making high-precision, millisecond-level docking communications with some programs written by Java, you will need to use PHP to output milliseconds of time. The approach I took earlier was to use an imprecise approach, which was to add a three-digit number to the PHP native time function. To get a more precise millisecond timestamp, use the following code:
function Getmillisecond () {list ($s 1, $s 2) = explode (' ', Microtime ()); return (float) sprintf ('%.0f ', (Floatval ($s 1) + floatval ($s 2)) * 1000); }/* * Gets the time difference, the millisecond level */function get_subtraction () {$t 1 = microtime (true); $t 2 = Microtime (true); Return (($t 2-$1) *1000). ' Ms '; }/* * microsecond microseconds millisecond milliseconds * Returns the number of milliseconds for a timestamp */function Get_millisecond () { List ($usec, $sec) = Explode ("", Microtime ()); $msec =round ($usec *1000); return $msec; }/* * Returns the number of milliseconds for a string timestamp */function Get_total_millisecond () {$time = Explo De ("", Microtime ()); $time = $time [1]. ($time [0] * 1000); $time 2 = Explode (".", $time); $time = $time 2 [0]; return $time; }/* * Returns the current Unix timestamp and the number of microseconds (represented by decimals in seconds) floating-point numbers, commonly used to calculate code snippet execution time*/function Microtime_float () {list ($usec, $sec) = Explode ("", Microtime ()); return (float) $usec + (float) $sec); } $millisecond = Get_millisecond (); $millisecond = Str_pad ($millisecond, 3, ' 0 ', str_pad_right); echo Date ("Ymdhis"). $millisecond;
PS: In 32-bit systems, the int maximum value of PHP is much smaller than the number of milliseconds, so the int type cannot be used, and PHP does not have a long type, so it has to be represented by floating-point numbers. Due to the use of floating-point numbers, if the accuracy is not set correctly, the results obtained using the echo display may not be correct, and the accuracy setting cannot be less than 13 bits if you want to see the correct output result.