우리는 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>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>
>
실행 결과는 다음과 같습니다.
<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 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.