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><?<span>php</span></span>
<span>//Function to get millisecond timestamp</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>//The above function is from Baidu, I didn’t understand it very well at first.</span>
<span>//Now the detailed explanation is as follows:</span>
<span>function<span> getMillisecond_new<span>(){</span></span></span>
<span> <span>//Use microtime() to get the microsecond timestamp, format (separated by spaces): 'fractional part of seconds, integer part of seconds', such as '0.69718900 1420440552'</span></span>
<span> <span>//explode the microsecond string and receive $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>//Convert to floating point number</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>//Add ×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>//Rounding</span></span>
<span> $total<span>=<span>round<span>(<span>$total<span>,<span>0<span>);</span></span></span></span></span></span></span></span>
<span> <span>//Return results</span></span>
<span> <span>return<span> $total<span>;</span></span></span></span>
<span>}</span>
<span>echo getMillisecond<span>(),<span>" PHP millisecond-getMillisecond()<br/>"<span>;</span></span>
<span>echo getMillisecond_new<span>().<span>' PHP millisecond-getMillisecond_new()'<span>;</span></span></span></span>
<span>/*</span>
<span> * Idea:</span>
<span> * 1. Use microtime() to get the microsecond timestamp, format: 0.69718900 1420440552</span>
<span> * 2. Add the front and back parts × 1000, then round to round($float,0)</span>
<span> * Seconds time()-->Milliseconds-->Microseconds microtime(), the interval between two is 1000 decimals</span>
<span> * In this way, it can be consistent with the timestamp of the front-end JS</span>
<span> * JS: new Date().getTime() gets the millisecond timestamp</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 gets millisecond timestamp'<span>);</span></span></span></span></span></span></span></span>
<span></<span>script</span></span>
>
<span></<span>body</span></span>
>
<span></<span>html</span></span>
>
The running results are as follows:
<span>1424069168633<span> PHP milli<span>second-<span>getMillisecond<span>()</span></span></span></span></span>
<span>1424069168633<span> PHP milli<span>second-<span>getMillisecond_new<span>()</span></span></span></span></span>
<span>1424069168643<span> JS<span>Get millisecond timestamp</span></span></span>
It can be seen that the third timestamp value is slightly larger than the first two. This is the time consumed by the code running, which is normal.
The above introduces PHP to obtain millisecond timestamps, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.