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 milliseconds
It 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.