PHP gets the timestamp in milliseconds, and php gets the timestamp in milliseconds.

Source: Internet
Author: User

PHP gets the timestamp in milliseconds, and php gets the timestamp in milliseconds.

We know that the timestamp obtained by the time () function in PHP is measured in seconds.

However, the timestamp obtained by the front-end JS, in milliseconds. in practice, how can we unify the timestamps of JS and PHP, that is, how to use PHP to obtain the timestamp in milliseconds? See the following example: <?php // Function to obtain the timestamp in milliseconds function getMillisecond() { list($t1, $t2) = explode(' ', microtime()); return (float)sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000); }   // The above functions are from Baidu. I didn't quite understand them at the beginning. // Separate the description as follows: function getMillisecond_new(){ // Use microtime () to obtain the microsecond timestamp. Format (separated by spaces): 'Second's fractional part of the second's integer part', for example, '0. 69718900 1420440552' // Blow up the microsecond string explode and receive $ t1 = 0.69718900 $ t2 = 1420440552 list($t1, $t2) = explode(' ', microtime()); // Convert it to a floating point number $t1=floatval($t1); $t2=floatval($t2); // Add x 1000 $total=( $t1+ $t2) * 1000; // Rounding $total=round($total,0); // Return results return $total; } Echo getMillisecond (), "PHP millisecond-getMillisecond () <br/> "; Echo getMillisecond_new (). 'php millisecond-getMillisecond_new ()'; /* * Idea: * 1. Use microtime () to obtain the microsecond timestamp. Format: 0.69718900 1420440552 * 2. Add the first and second parts x 1000, and round them to round ($ float, 0) * Second time () --> millisecond --> microsecond microtime (), which is 1000 hexadecimal between two * In this way, it can be consistent with the front-end JS timestamp. * JS: new Date (). getTime () obtains the timestamp in milliseconds. */ ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> > > <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>time</title> > <body> <br /> <script> var time=new Date(); var mtime=time.getTime(); Document. write (mtime + 'js get timestamp in milliseconds '); </script> </body> > The running result is as follows: 1424069168633 PHP millisecond-getMillisecond () 1424069168633 PHP millisecond-getMillisecond_new () 1424069168643 JS get the timestamp in millisecondsIt can be seen that the third timestamp value is slightly larger than the first two, which is the time consumed by code running and is normal.

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.