筑路设备租赁 發表於 2019-8-19 15:52:00

【php】php获取当前毫秒时间戳

<p>最近在做一个智能家居项目的后台,需要实时上传用户对智能设备的配置信息到服务器,以便实现同步,因此对于时间的精确度要求比较高,最开始直接是用php的time()函数来获取时间戳,获取的时间精确到秒级别,如果客户端同时操作的话还是有可能产生冲突,因此建议将时间戳精度提高到毫秒级别,但是在php没有自带的函数能获取毫秒时间戳,但提供了一个microtime()函数,如果调用时不带可选参数,本函数以 "msec sec" 的格式返回一个字符串,其中 sec 是自 Unix 纪元(0:00:00 January 1, 1970 GMT)起到现在的秒数,msec 是微秒部分。字符串的两部分都是以秒为单位返回的。</p>
<p>microtime()函数的详细说明,可以到w3chool上面看看《PHP microtime() 函数》。</p>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre>&lt;?<span style="color: rgba(0, 0, 0, 1)">php
</span><span style="color: rgba(0, 0, 255, 1)">echo</span> <span style="color: rgba(0, 128, 128, 1)">microtime</span><span style="color: rgba(0, 0, 0, 1)">();
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">输出结果是
//0.25139300 1138197510</span></pre>
</div>
<p>&nbsp;</p>
<p>&nbsp;注意了,它的结果是分两部分的,也就是前半部分是毫秒(但是单位是秒),后半部分是秒。<br>现在,我们依据这个做下修改,如下:<br>&nbsp;</p>
<div class="cnblogs_code">
<pre>&lt;?<span style="color: rgba(0, 0, 0, 1)">php
</span><span style="color: rgba(0, 0, 255, 1)">list</span>(<span style="color: rgba(128, 0, 128, 1)">$msec</span>, <span style="color: rgba(128, 0, 128, 1)">$sec</span>) = <span style="color: rgba(0, 128, 128, 1)">explode</span>(' ', <span style="color: rgba(0, 128, 128, 1)">microtime</span><span style="color: rgba(0, 0, 0, 1)">());
</span><span style="color: rgba(128, 0, 128, 1)">$msectime</span> = (<span style="color: rgba(0, 0, 255, 1)">float</span>)<span style="color: rgba(0, 128, 128, 1)">sprintf</span>('%.0f', (<span style="color: rgba(0, 128, 128, 1)">floatval</span>(<span style="color: rgba(128, 0, 128, 1)">$msec</span>) + <span style="color: rgba(0, 128, 128, 1)">floatval</span>(<span style="color: rgba(128, 0, 128, 1)">$sec</span>)) * 1000);</pre>
</div>
<p>&nbsp;</p>
<p>这样就可以了,$msectime就是当前的毫秒数!可以将这两行封装成一个函数方便使用。</p>
<div class="cnblogs_code">
<pre>&lt;?<span style="color: rgba(0, 0, 0, 1)">php
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">返回当前的毫秒时间戳</span>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> msectime() {
  </span><span style="color: rgba(0, 0, 255, 1)">list</span>(<span style="color: rgba(128, 0, 128, 1)">$msec</span>, <span style="color: rgba(128, 0, 128, 1)">$sec</span>) = <span style="color: rgba(0, 128, 128, 1)">explode</span>(' ', <span style="color: rgba(0, 128, 128, 1)">microtime</span><span style="color: rgba(0, 0, 0, 1)">());
  </span><span style="color: rgba(128, 0, 128, 1)">$msectime</span> = (<span style="color: rgba(0, 0, 255, 1)">float</span>)<span style="color: rgba(0, 128, 128, 1)">sprintf</span>('%.0f', (<span style="color: rgba(0, 128, 128, 1)">floatval</span>(<span style="color: rgba(128, 0, 128, 1)">$msec</span>) + <span style="color: rgba(0, 128, 128, 1)">floatval</span>(<span style="color: rgba(128, 0, 128, 1)">$sec</span>)) * 1000<span style="color: rgba(0, 0, 0, 1)">);<br><br>  return $msectime;
}</span></pre>
</div>
<p>&nbsp;</p>
<p>注意:sprintf('%.0f', $num) 是输出不含小数部分的浮点数</p>
<p>事情还没有结束,我把时间戳改成毫秒级别后,再次更新数据库数据时,却提示超出范围,原来之前我在数据库中是用int型来存储time()函数获取的秒级别的时间戳,存储范围是够的,改成毫秒级别的,就得改成BIGINT类型了。</p>
<p>&nbsp;</p>
<p><strong>整数类型 &nbsp; &nbsp; &nbsp; &nbsp; 字节 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;范围(有符号) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;范围(无符号) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;用途&nbsp;</strong></p>
<p>TINYINT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1字节 &nbsp; &nbsp; &nbsp; &nbsp;(-128,127) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (0,255) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;小整数值&nbsp;</p>
<p>SMALLINT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2字节 &nbsp; &nbsp; (-32 768,32 767) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(0,65 535) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 大整数值&nbsp;</p>
<p>MEDIUMINT &nbsp; &nbsp; &nbsp; &nbsp; 3字节 &nbsp; &nbsp;(-8 388 608,8 388 607) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (0,16 777 215) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 大整数值&nbsp;</p>
<p>INT或INTEGER &nbsp; &nbsp; &nbsp;4字节 &nbsp; (-2 147 483 648,2 147 483 647) &nbsp; &nbsp; &nbsp; (0,4 294 967 295) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;大整数值&nbsp;</p>
<p>BIGINT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;8字节 &nbsp; (-9 233 372 036 854 775 808,9 223 372 036 854 775 807) (0,18 446 744 073 709 551 615) 极大整数值&nbsp;</p><br><br>
来源:https://www.cnblogs.com/opensmarty/p/11377624.html
頁: [1]
查看完整版本: 【php】php获取当前毫秒时间戳