PHP の time() 関数によって取得されるタイムスタンプは秒単位であることがわかっています。
ただし、フロントエンド JS によって取得されるタイムスタンプはミリ秒単位です。 では、実際のアプリケーションでは、JS と PHP のタイムスタンプを統合する方法、つまり、PHP を使用してミリ秒のタイムスタンプを取得する方法については、次の例を参照してください。<span class="pun"><?<span class="pln">php</span></span>
<span class="pln">//函数,获取毫秒时间戳</span>
<span class="kwd">function<span class="pln"> getMillisecond<span class="pun">()<span class="pln"> <span class="pun">{</span></span></span></span></span>
<span class="pln"> list<span class="pun">(<span class="pln">$t1<span class="pun">,<span class="pln"> $t2<span class="pun">)<span class="pln"> <span class="pun">=<span class="pln"> explode<span class="pun">(<span class="str">' '<span class="pun">,<span class="pln"> microtime<span class="pun">());</span></span></span></span></span></span></span></span></span></span></span></span></span></span>
<span class="pln"> <span class="kwd">return<span class="pln"> <span class="pun">(<span class="kwd">float<span class="pun">)<span class="pln">sprintf<span class="pun">(<span class="str">'%.0f'<span class="pun">,<span class="pln"> <span class="pun">(<span class="pln">floatval<span class="pun">(<span class="pln">$t1<span class="pun">)<span class="pln"> <span class="pun">+<span class="pln"> floatval<span class="pun">(<span class="pln">$t2<span class="pun">))<span class="pln"> <span class="pun">*<span class="pln"> <span class="lit">1000<span class="pun">);</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 class="pun">}</span>
<span class="com">//上面的函数是百度出来的,我刚开始看着也不是很明白.</span>
<span class="com">//现分开详细讲解如下:</span>
<span class="kwd">function<span class="pln"> getMillisecond_new<span class="pun">(){</span></span></span>
<span class="pln"> <span class="com">//使用microtime()获取微秒时间戳,格式(中间空格隔开):'秒的小数部分 秒的整数部分',例如'0.69718900 1420440552'</span></span>
<span class="pln"> <span class="com">//将微秒字符串explode炸开,接收$t1=0.69718900 $t2=1420440552</span></span>
<span class="pln"> list<span class="pun">(<span class="pln">$t1<span class="pun">,<span class="pln"> $t2<span class="pun">)<span class="pln"> <span class="pun">=<span class="pln"> explode<span class="pun">(<span class="str">' '<span class="pun">,<span class="pln"> microtime<span class="pun">());</span></span></span></span></span></span></span></span></span></span></span></span></span></span>
<span class="pln"> <span class="com">//转成浮点数</span></span>
<span class="pln"> $t1<span class="pun">=<span class="pln">floatval<span class="pun">(<span class="pln">$t1<span class="pun">);</span></span></span></span></span></span>
<span class="pln"> $t2<span class="pun">=<span class="pln">floatval<span class="pun">(<span class="pln">$t2<span class="pun">);</span></span></span></span></span></span>
<span class="pln"> <span class="com">//相加×1000</span></span>
<span class="pln"> $total<span class="pun">=(<span class="pln"> $t1<span class="pun">+<span class="pln"> $t2<span class="pun">)<span class="pln"> <span class="pun">*<span class="pln"> <span class="lit">1000<span class="pun">;</span></span></span></span></span></span></span></span></span></span></span>
<span class="pln"> <span class="com">//四舍五入</span></span>
<span class="pln"> $total<span class="pun">=<span class="pln">round<span class="pun">(<span class="pln">$total<span class="pun">,<span class="lit">0<span class="pun">);</span></span></span></span></span></span></span></span>
<span class="pln"> <span class="com">//返回结果</span></span>
<span class="pln"> <span class="kwd">return<span class="pln"> $total<span class="pun">;</span></span></span></span>
<span class="pun">}</span>
<span class="pln">echo getMillisecond<span class="pun">(),<span class="str">" PHP millisecond-getMillisecond()<br/ >"<span class="pun">;<code class="language-php"><span class="pln">echo getMillisecond<span class="pun">(),<span class="str">" PHP毫秒-getMillisecond()<br/>"<span class="pun">;</span></span></span></span>
<span class="pln">echo getMillisecond_new<span class="pun">().<span class="str">' PHP毫秒-getMillisecond_new()'<span class="pun">;</span></span></span></span>
<span class="com">/*</span>
<span class="com"> * 思路:</span>
<span class="com"> * 1.使用microtime()获取微秒时间戳,格式:0.69718900 1420440552</span>
<span class="pln">echo getMillisecond_new<span class="pun">().<span class="str">' PHP millisecond-getMillisecond_new()'<span class="ダジャレ">;<code class="language-php"><span class="com"> * 2.前后两部分相加×1000,然后四舍五入round($float,0)</span>
<span class="com"> * 秒time()-->毫秒-->微秒microtime(),两两之间是1000进制</span>
<span class="com"> * 这样,就可以与前端JS的时间戳保持一致</span>
<span class="com"> * JS : new Date().getTime()获得毫秒时间戳</span>
<span class="com"> */</span>
<span class="com">/*<code class="language-php"><span class="pun">?></span>
<span class="pun"><!<span class="pln">DOCTYPE HTML PUBLIC <span class="str">"-//W3C//DTD HTML 4.01 Transitional//EN"</span></span></span>
<span class="com"> * アイデア:<code class="language-php"><span class="pln"><html</span>
<span class="pln"><head</span>
<span class="com"> * 1. microtime() を使用してマイクロ秒のタイムスタンプを取得します。形式: 0.69718900 1420440552<code class="language-php"><span class="pln"><meta http<span class="pun">-<span class="pln">equiv<span class="pun">=<span class="str">"Content-Type"<span class="pln"> content<span class="pun">=<span class="str">"text/html; charset=utf-8"</span></span></span></span></span></span></span></span>
<span class="pln"><title><span class="pln">time<<span class="pun">/<span class="pln">title</span></span></span></span>
<span class="com"> * 2. 前後の部分を×1000で加算し、round($float,0)に四捨五入します<code class="language-php"><span class="pun"></<span class="pln">head</span></span>
<span class="pln"><body</span>
<span class="com"> * 秒 time()-->ミリ秒 -->マイクロ秒 microtime()、2 つの間隔は小数点以下 1000 桁です<code class="language-php"><span class="pln"><br <span class="pun">/</span></span>
<span class="pln"><script</span>
<span class="com"> * このようにして、フロントエンド JS のタイムスタンプと一致させることができます<code class="language-php"><span class="kwd">var<span class="pln"> time<span class="pun">=<span class="kwd">new<span class="pln"> <span class="typ">Date<span class="pun">();</span></span></span></span></span></span></span>
<span class="kwd">var<span class="pln"> mtime<span class="pun">=<span class="pln">time<span class="pun">.<span class="pln">getTime<span class="pun">();</span></span></span></span></span></span></span>
<span class="com"> * JS: new Date().getTime() はミリ秒のタイムスタンプを取得します<code class="language-php"><span class="pln">document<span class="pun">.<span class="pln">write<span class="pun">(<span class="pln">mtime<span class="pun">+<span class="str">' JS获得毫秒时间戳'<span class="pun">);</span></span></span></span></span></span></span></span>
<span class="pun"></<span class="pln">script</span></span>
<span class="com"> */<code class="language-php"><span class="pun"></<span class="pln">body</span></span>
<span class="pun"></<span class="pln">html</span></span>
<span class="pun">?><code class="language-php"><span class="lit">1424069168633<span class="pln"> PHP毫<span class="pun">秒-<span class="pln">getMillisecond<span class="pun">()</span></span></span></span></span>
<span class="lit">1424069168633<span class="pln"> PHP毫<span class="pun">秒-<span class="pln">getMillisecond_new<span class="pun">()</span></span></span></span></span>
<span class="pun"><!<span class="pln">DOCTYPE HTML PUBLIC <span class="str">"-//W3C//DTD HTML 4.01暫定//EN「<code class="language-php"><span class="lit">1424069168643<span class="pln"> JS<span class="pun">获得毫秒时间戳</span></span></span>