PHP gets millisecond timestamp, PHP gets milliseconds
We know that the time () function in PHP gets the timestamp, in seconds.
However, the front-end JS gets the timestamp in milliseconds. So, in the actual application, how to use the time stamp JS and PHP Unified, that is, how to get millisecond timestamp using PHP, see the following example:
php
//函数,获取毫秒时间戳
function getMillisecond() {
List ( $t 1,< Span class= "PLN" > $t 2) = Explode ( ", Microtime ());
return (float)sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
}
//上面的函数是百度出来的,我刚开始看着也不是很明白.
//现分开详细讲解如下:
function getMillisecond_new(){
//使用microtime()获取微秒时间戳,格式(中间空格隔开):'秒的小数部分 秒的整数部分',例如'0.69718900 1420440552'
//将微秒字符串explode炸开,接收$t1=0.69718900 $t2=1420440552
list($t1, $t2) = explode(' ', microtime());
//转成浮点数
$t1=floatval($t1);
$t2=floatval($t2);
//相加×1000
$total=( $t1+ $t2) * 1000;
//四舍五入
$total=round($total,0);
//返回结果
return $total;
}
echo getMillisecond()," PHP毫秒-getMillisecond()
";
echo getMillisecond_new().' PHP毫秒-getMillisecond_new()';
/*
* 思路:
* 1.使用microtime()获取微秒时间戳,格式:0.69718900 1420440552
* 2.前后两部分相加×1000,然后四舍五入round($float,0)
* 秒time()-->毫秒-->微秒microtime(),两两之间是1000进制
* 这样,就可以与前端JS的时间戳保持一致
* JS : new Date().getTime()获得毫秒时间戳
*/
?>
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
>
<>< span=""><>
>
<>< span=""><>
>
-equiv="Content-Type" content="text/html; charset=utf-8"
>
time</title
>
head
>
<>< span=""><>
>
/
>
<>< span=""><>
>
var time=new Date();
var mtime=time.getTime();
document.write(mtime+' JS获得毫秒时间戳');
script
>
body
>
html
The > operation results are as follows:
1424069168633 PHP毫秒-getMillisecond()
1424069168633 PHP毫秒-getMillisecond_new()
1424069168643 JS获得毫秒时间戳
As you can see, the third timestamp value is slightly larger than the first two, which is the time it takes for the code to run and is normal.
http://www.bkjia.com/PHPjc/971768.html www.bkjia.com true http://www.bkjia.com/PHPjc/971768.html techarticle PHP gets millisecond timestamp, PHP gets milliseconds we know that the time () function in PHP gets the timestamp, which is in seconds. However, the front-end JS gets the timestamp in milliseconds. Well, in practical applications ...