但是,前端js获取的时间戳,单位是毫秒.那么,在实际应用中,如何将js和php的时间戳统一,即如何使用php获取毫秒时间戳呢,请看下例:php//函数,获取毫秒时间戳function getmillisecond() { list($t1, $t2) = 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>>>-equiv=content-type content=text/html; charset=utf-8>time/title>head>>/>>var time=new date();var mtime=time.gettime();document.write(mtime+' js获得毫秒时间戳');script>body>html>运行结果如下:1424069168633 php毫秒-getmillisecond()1424069168633 php毫秒-getmillisecond_new()1424069168643 js获得毫秒时间戳可以看出,第三个时间戳数值稍微比前两个大,这是代码运行所消耗的时间,是正常的.
http://www.bkjia.com/phpjc/971768.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/971768.htmltecharticlephp获取毫秒时间戳,php获取毫秒 我们知道,php中time()函数获取的时间戳,其单位是秒. 但是,前端js获取的时间戳,单位是毫秒.那么,在实际应用...
