We know that the timestamp obtained by the time() function in PHP is in seconds.
However, the timestamp obtained by the front-end JS is in milliseconds. So, in practical applications, how to unify the timestamps of JS and PHP, that is, how to use PHP to obtain millisecond timestamps? Please see the following example:<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="pun">;<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"> * Idea:<code class="language-php"><span class="pln"><html</span>
<span class="pln"><head</span>
<span class="com"> * 1. Use microtime() to obtain the microsecond timestamp, format: 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. Add the front and back parts by ×1000, then round ($float,0)<code class="language-php"><span class="pun"></<span class="pln">head</span></span>
<span class="pln"><body</span>
<span class="com"> * Second time()-->Millisecond-->Microsecond microtime(), the interval between two is 1000 decimal<code class="language-php"><span class="pln"><br <span class="pun">/</span></span>
<span class="pln"><script</span>
<span class="com"> * In this way, it can be consistent with the timestamp of the front-end 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() gets the millisecond timestamp<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 Transitional//EN"<code class="language-php"><span class="lit">1424069168643<span class="pln"> JS<span class="pun">获得毫秒时间戳</span></span></span>