Home >Backend Development >PHP Tutorial >PHP program code to get the number of milliseconds of the current time_PHP tutorial
To get the time and date in php, we can use the date function. If we want to get milliseconds, we can use time but it cannot be separated out specifically. Let me introduce some examples of php getting the milliseconds of the current time.
PHP itself does not provide a function that returns the number of milliseconds, but it provides a microtime() function, which returns an array containing two elements, one is the number of seconds and the other is the number of milliseconds expressed as a decimal. With the help of this function, It's easy to define a function that returns the number of milliseconds, for example:
The code is as follows | Copy code | ||||
List($s1, $s2) = explode(' ', microtime()); Return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000); }
|
代码如下 | 复制代码 |
/* * microsecond 微秒 millisecond 毫秒 *返回时间戳的毫秒数部分 */ function get_millisecond() { list($usec, $sec) = explode(" ", microtime()); $msec=round($usec*1000); return $msec; } /* * *返回字符串的毫秒数时间戳 */ function get_total_millisecond() { $time = explode (" ", microtime () ); $time = $time [1] . ($time [0] * 1000); $time2 = explode ( ".", $time ); $time = $time2 [0]; return $time; } /* * *返回当前 Unix 时间戳和微秒数(用秒的小数表示)浮点数表示,常用来计算代码段执行时间 */ function microtime_float() { list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } |
It should be noted that in 32-bit systems, the maximum int value of PHP is far less than the number of milliseconds, so the int type cannot be used, and there is no long type in PHP, so it has to be represented by floating point numbers.