首頁 > 後端開發 > php教程 > PHP取得毫秒時間戳

PHP取得毫秒時間戳

WBOY
發布: 2016-08-08 09:27:46
原創
1019 人瀏覽過

我們知道,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>' 'H <span><span><span></span></span></span></span></span> </span> </span>(</span></span> </span>return</span> </span>(</span>float</span>)sprintf

(

'%.0f',<span> <span>(<span>f <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></span></span>//上面的函數是百度出來的,我剛開始看著也不是很明白.</span></span></span> </span></span></span>//現分開詳細講解如下:</span>

<span>function</span> getMillisecond_new(){

<span> </span>//使用microtime()取得微秒時間戳,格式(中間空格隔開):'秒的小數部分 秒的整數部分',例如'0.69718900 1420440552'

<span></span>

<span></span>

<span></span>

//將微秒字串explode炸開,接收$t1=0.69718900 $t2=1420440552<span></span>

list($t1, $t2) = explode(' 'H <span></span> //轉成浮點數

$t1

=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></span> </span>

$total

=( $t1<span>+<span> $t2</span>)</span> *

1000; //四捨五入 <span></span> $total=round($total,0);

);<span></span>);

<span><span><span>);<span><span><span><span><span> </span></span></span> </span>//回傳結果</span></span></span></span>

return<span> $total<span>;</span></span>

}<span></span> echo getMillisecond

(),

" PHP毫秒-getMillisecond()
";<span></span>

<span><span><span>echo getMillisecond_new<span>().</span>' PHP毫秒-getMillisecond_new()'</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

"-//W3C//DTD HTML 4.01 Transitional//EN"<span></span>>

<span><span>></span> </span>

> <span></span>-

equiv=<span>"Content-Type"</span> content=

"text/html; charset=utf-8"

<span></span>

<span><span><span><span> </span></span></span><title></title></span>time/

title

<span></span>>

head<span></span>>

<span></span>>


/<span></span>>

<span><script><span>><span> <span><span><span>var</script></span> time=new Date();

🎜🎜🎜🎜🎜🎜🎜🎜🎜🎜🎜

<span>var<span> mtime<span>=<span>time<span>.<span>getTime<span>();</span></span></span></span></span></span></span>

<span></span>document.write(mtime+' JS取得毫秒時間戳');

<span></span>script

> <span></span>body

> <span></span>html

>

運行結果如下:

<span> <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>1424069168643 JS取得毫秒時間戳

可以看出,第三個時間戳數值稍微比前兩個大,這是代碼運行所消耗的時間,是正常的.

以上就介紹了PHP獲取毫秒時間戳,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。 🎜 🎜 🎜
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板