Wir wissen, dass der von der Funktion time() in PHP erhaltene Zeitstempel in Sekunden angegeben ist.
Der vom Front-End-JS erhaltene Zeitstempel ist jedoch in Millisekunden angegeben.
Wie vereinheitlicht man also in praktischen Anwendungen die Zeitstempel von JS und PHP, das heißt, wie erhält man mit PHP Zeitstempel im Millisekundenbereich?
<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>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><!<span>DOCTYPE HTML PUBLIC <span>"-//W3C//DTD HTML 4.01 Transitional//EN"</span></span></span>
><span><html</span>
><span><head</span>
><span><meta http<span>-<span>equiv<span>=<span>"Content-Type"<span> content<span>=<span>"text/html; charset=utf-8"</span></span></span></span></span></span></span></span>
><span><title><span>time<<span>/<span>title</span></span></span></span>
><span></<span>head</span></span>
><span><body</span>
><span><br <span>/</span></span>
><span><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>
>
Die Laufergebnisse sind wie folgt:
<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>
Es ist ersichtlich, dass der dritte Zeitstempelwert etwas größer ist als die ersten beiden. Dies ist die Zeit, die der Code für die Ausführung benötigt, was normal ist.
Das Obige stellt PHP vor, um Millisekunden-Zeitstempel zu erhalten, einschließlich einiger Aspekte des Inhalts. Ich hoffe, es wird für Freunde hilfreich sein, die sich für PHP-Tutorials interessieren.