Php does not provide a function that returns the number of milliseconds, but provides a microtime () function that returns an array containing two elements, one being the number of seconds, one is the number of milliseconds represented by decimals. With this function, you can easily define a function that returns the number of milliseconds. For example:
The code is as follows: |
Copy code |
Function getMillisecond (){ List ($ s1, $ s2) = explode ('', microtime ()); Return (float) sprintf ('%. 0f', (floatval ($ s1) + floatval ($ s2) * 1000 ); } |
Note that in a 32-bit system, the maximum int value of php is much smaller than the number of milliseconds, so the int type cannot be used, but php does not have the long type, so it must be represented by a floating point number. Because floating point numbers are used, if the precision setting is incorrect, the obtained result may be incorrect when echo is used. To view the correct output result, the precision setting cannot be less than 13 characters.
The code is as follows: |
Copy code |
/* * Microsecond millisecond * Returns the timestamp in milliseconds. */ Function get_millisecond () { List ($ usec, $ sec) = explode ("", microtime ()); $ Msec = round ($ usec x 1000 ); Return $ msec; } /* * * Returns the string's timestamp in milliseconds. */ Function get_total_millisecond () { $ Time = explode ("", microtime ()); $ Time = $ time [1]. ($ time [0] * 1000 ); $ Time2 = explode (".", $ time ); $ Time = $ time2 [0]; Return $ time; } /* * * Returns the current Unix timestamp and the number of microseconds (expressed in seconds decimal places). It is often used to calculate the execution time of code segments. */ Function microtime_float () { List ($ usec, $ sec) = explode ("", microtime ()); Return (float) $ usec + (float) $ sec ); }
|
Note that in a 32-bit system, the maximum int value of php is much smaller than the number of milliseconds, so the int type cannot be used, but php does not have the long type, so it must be represented by a floating point number.