PHP program code to get the number of milliseconds of the current time_PHP tutorial

WBOY
Release: 2016-07-13 16:57:55
Original
1090 people have browsed it

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
 代码如下 复制代码

function getMillisecond() {
    list($s1, $s2) = explode(' ', microtime());
    return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
}

function getMillisecond() {
List($s1, $s2) = explode(' ', microtime());
Return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
}

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. Due to the use of floating point numbers, if the precision setting is incorrect, the obtained results may not be displayed correctly when using echo. To see the correct output, the precision setting cannot be lower than 13 digits.
 代码如下 复制代码
        /*
         * 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631498.htmlTechArticleTo get the time and date in php, we can use the date function. If you want to get milliseconds, you can use time but it cannot be separated out specifically. Below I will introduce some examples of obtaining the current time milliseconds in PHP. ...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template