PHP获取毫秒级时间戳
文章来自:https://www.cnblogs.com/opensmarty/p/11377624.html
我们php通过time()函数获取到的时间戳是精确到秒的,当我们有些业务场景需要用到毫秒级的时间戳,那么我们就需要用如下方式进行获取。
microtime(): 能获取到php的毫秒,但是获取的数据分两部分的:
0.25139300 1138197510 前半部分是毫秒(以秒为单位的小数),后半部分是秒
通过如下php代码转换组装便可以获得精确的毫秒时间戳
list($msec, $sec) = explode(’ ', microtime());
m
s
e
c
t
i
m
e
=
(
f
l
o
a
t
)
s
p
r
i
n
t
f
(
′
msectime = (float)sprintf('%.0f', (floatval(
msectime=(float)sprintf(′msec) + floatval($sec)) * 1000);