Php method instance code to get millisecond timestamp

Source: Internet
Author: User
Tags echo date echo display
This article mainly introduces the method of getting the millisecond timestamp of PHP, which involves the processing technique of PHP to return the result of microtime function, it is very simple and practical, the friend who needs can refer to the following

The example in this paper describes how PHP gets the millisecond timestamp. Share to everyone for your reference. The specific analysis is as follows:

PHP itself does not provide a function to get a millisecond timestamp, which can be obtained by gettime () in Java. If you are making high-precision, millisecond-level docking communications with some programs written by Java, you will need to use PHP to output milliseconds of time. The approach I took earlier was to use an imprecise approach, which was to add a three-digit number to the PHP native time function. To get a more precise millisecond timestamp, use the following code:

function Getmillisecond () {list ($s 1, $s 2) = explode (' ', Microtime ()); return (float) sprintf ('%.0f ', (Floatval ($s 1) + floatval ($s 2)) * 1000);              }/* * Gets the time difference, the millisecond level */function get_subtraction () {$t 1 = microtime (true);              $t 2 = Microtime (true); Return (($t 2-$1) *1000). '      Ms ';              }/* * microsecond microseconds millisecond milliseconds * Returns the number of milliseconds for a timestamp */function Get_millisecond () {              List ($usec, $sec) = Explode ("", Microtime ());              $msec =round ($usec *1000);                     return $msec; }/* * Returns the number of milliseconds for a string timestamp */function Get_total_millisecond () {$time = Explo               De ("", 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);    } $millisecond = Get_millisecond ();    $millisecond = Str_pad ($millisecond, 3, ' 0 ', str_pad_right); echo Date ("Ymdhis"). $millisecond;

PS: In 32-bit systems, the int maximum value of PHP 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.

Related Article

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.