Php code for obtaining the current time in milliseconds

Source: Internet
Author: User
Tags current time explode php code time in milliseconds

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.

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.