Php method for obtaining the current time in milliseconds

Source: Internet
Author: User
Tags time in milliseconds
Php does not provide a function that returns the number of milliseconds, but provides a microtime () function. with this function, it is easy to define a function that returns milliseconds. php itself does not provide a function that returns milliseconds, but provides a microtime () function that returns an array containing two elements, one is the number of seconds and the other is the number of milliseconds in decimal places. with this function, you can easily define a function that returns the number of milliseconds. for example:
The code is as follows:
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.

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.