PHP gets the number of milliseconds in the current time program code _php tutorial

Source: Internet
Author: User
Tags echo display
Get time and date in PHP we can use the DATE function, and if the milliseconds can be used but not specific, let me introduce some examples of PHP getting the current time milliseconds.

PHP itself does not provide a function to return milliseconds, but it provides a microtime () function that returns an array containing two elements, a number of seconds, and a fractional number of milliseconds, which makes it easy to define a function that returns the number of milliseconds, for example:

The code is as follows Copy Code

function Getmillisecond () {
List ($s 1, $s 2) = explode (", microtime ());
return (float) sprintf ('%.0f ', (Floatval ($s 1) + floatval ($s 2)) * 1000);
}

It is important to note that the Int maximum value of PHP in a 32-bit system 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.

code as follows copy code
/*
* Microsecond microsecond Millisecond ms
* Returns the number of milliseconds for the timestamp
*/
function Get_millisecond ()
{
List ($usec, $sec) = Explode ("", Microtime ());
$msec =round ($usec *1000);
return $msec;

}

/*
*
* Returns the number of milliseconds in a string timestamp
*/
function Get_total_millisecond ()
{
$time = Explode ("", 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);
}

It is important to note that the Int maximum value of PHP in a 32-bit system 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.

http://www.bkjia.com/PHPjc/631498.html www.bkjia.com true http://www.bkjia.com/PHPjc/631498.html techarticle get time and date in PHP we can use the DATE function, and if the milliseconds can be used but not specific, let me introduce some examples of PHP getting the current time milliseconds. ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.