PHP获取毫秒级时间戳的方法

PHP本身没有提供获取毫秒级时间戳的函数,java里面可以通过gettime();获取。 如果是要与java写的某些程序进行高精度的毫秒级的对接通信,则需要使用PHP输出毫秒级的时间。 之前我采取的方法是采用不精准的方式,也就是在PHP原生的时间函数后面加上一个三位数字构成。为获取更为精准的毫秒级时间戳可以使用下面的代码: function getMillisecond() { list($t1, $t2) = explode(' ', microtime()); return (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000); } echo getMillisecond(); **microtime()函数** 如果调用时不带可选参数,本函数以 "msec sec" 的格式返回一个字符串,其中 sec 是自 Unix 纪元(0:00:00 January 1, 1970 GMT)起到现在的秒数,msec 是微秒部分。 microtime(true)返回的值是sec+msec的和,保留四位小数。 microtime()返回值类型是string(21),microtime(true)返回值类型是float。 函数可写为: function getMillisecond() { $time = microtime(true); return (float)sprintf('%.0f',$time*1000); }
联系我们

邮箱 626512443@qq.com
电话 18611320371(微信)
QQ群 235681453

Copyright © 2015-2022

备案号:京ICP备15003423号-3