Maison > Article > développement back-end > PHP获取毫秒时间戳
我们知道,PHP中time()函数获取的时间戳,其单位是秒.
但是,前端JS获取的时间戳,单位是毫秒.
那么,在实际应用中,如何将JS和PHP的时间戳统一,即如何使用PHP获取毫秒时间戳呢,请看下例:
<span><span>php</span></span>
<span>//函数,获取毫秒时间戳</span>
<span>function<span> getMillisecond<span>()<span> <span>{</span></span></span></span></span>
<span> list<span>(<span>$t1<span>,<span> $t2<span>)<span> <span>=<span> explode<span>(<span>' '<span>,<span> microtime<span>());</span></span></span></span></span></span></span></span></span></span></span></span></span></span>
<span> <span>return<span> <span>(<span>float<span>)<span>sprintf<span>(<span>'%.0f'<span>,<span> <span>(<span>floatval<span>(<span>$t1<span>)<span> <span>+<span> floatval<span>(<span>$t2<span>))<span> <span>*<span> <span>1000<span>);</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
<span>}</span>
<span>//上面的函数是百度出来的,我刚开始看着也不是很明白.</span>
<span>//现分开详细讲解如下:</span>
<span>function<span> getMillisecond_new<span>(){</span></span></span>
<span> <span>//使用microtime()获取微秒时间戳,格式(中间空格隔开):'秒的小数部分 秒的整数部分',例如'0.69718900 1420440552'</span></span>
<span> <span>//将微秒字符串explode炸开,接收$t1=0.69718900 $t2=1420440552</span></span>
<span> list<span>(<span>$t1<span>,<span> $t2<span>)<span> <span>=<span> explode<span>(<span>' '<span>,<span> microtime<span>());</span></span></span></span></span></span></span></span></span></span></span></span></span></span>
<span> <span>//转成浮点数</span></span>
<span> $t1<span>=<span>floatval<span>(<span>$t1<span>);</span></span></span></span></span></span>
<span> $t2<span>=<span>floatval<span>(<span>$t2<span>);</span></span></span></span></span></span>
<span> <span>//相加×1000</span></span>
<span> $total<span>=(<span> $t1<span>+<span> $t2<span>)<span> <span>*<span> <span>1000<span>;</span></span></span></span></span></span></span></span></span></span></span>
<span> <span>//四舍五入</span></span>
<span> $total<span>=<span>round<span>(<span>$total<span>,<span>0<span>);</span></span></span></span></span></span></span></span>
<span> <span>//返回结果</span></span>
<span> <span>return<span> $total<span>;</span></span></span></span>
<span>}</span>
<span>echo getMillisecond<span>(),<span>" PHP毫秒-getMillisecond()<br>"<span>;</span></span></span></span>
<span>echo getMillisecond_new<span>().<span>' PHP毫秒-getMillisecond_new()'<span>;</span></span></span></span>
<span>/*</span>
<span> * 思路:</span>
<span> * 1.使用microtime()获取微秒时间戳,格式:0.69718900 1420440552</span>
<span> * 2.前后两部分相加×1000,然后四舍五入round($float,0)</span>
<span> * 秒time()-->毫秒-->微秒microtime(),两两之间是1000进制</span>
<span> * 这样,就可以与前端JS的时间戳保持一致</span>
<span> * JS : new Date().getTime()获得毫秒时间戳</span>
<span> */</span>
<span>?></span>
<span>DOCTYPE HTML PUBLIC <span>"-//W3C//DTD HTML 4.01 Transitional//EN"</span></span>
>
<span></span>
>
<span></span>
>
<span><meta http>-<span>equiv<span>=<span>"Content-Type"<span> content<span>=<span>"text/html; charset=utf-8"</span></span></span></span></span></span></span>
>
<span><title><span>time/<span>title</span></span></title></span>
>
<span><span>head</span></span>
>
<span></span>
>
<span><br>/</span>
>
<span><script></script></span>
>
<span>var<span> time<span>=<span>new<span> <span>Date<span>();</span></span></span></span></span></span></span>
<span>var<span> mtime<span>=<span>time<span>.<span>getTime<span>();</span></span></span></span></span></span></span>
<span>document<span>.<span>write<span>(<span>mtime<span>+<span>' JS获得毫秒时间戳'<span>);</span></span></span></span></span></span></span></span>
<span><span>script</span></span>
>
<span><span>body</span></span>
>
<span><span>html</span></span>
>
运行结果如下:
<span>1424069168633<span> PHP毫<span>秒-<span>getMillisecond<span>()</span></span></span></span></span>
<span>1424069168633<span> PHP毫<span>秒-<span>getMillisecond_new<span>()</span></span></span></span></span>
<span>1424069168643<span> JS<span>获得毫秒时间戳</span></span></span>
可以看出,第三个时间戳数值稍微比前两个大,这是代码运行所消耗的时间,是正常的.
以上就介绍了PHP获取毫秒时间戳,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。