Heim > Backend-Entwicklung > PHP-Tutorial > PHP获取毫秒时间戳,php获取毫秒_PHP教程

PHP获取毫秒时间戳,php获取毫秒_PHP教程

WBOY
Freigeben: 2016-07-13 10:01:51
Original
965 Leute haben es durchsucht

PHP获取毫秒时间戳,php获取毫秒

我们知道,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毫秒-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="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="pun">?></span> <span class="pun">DOCTYPE HTML PUBLIC <span class="str">"-//W3C//DTD HTML 4.01 Transitional//EN"</span></span>> <span class="pln"></span>> <span class="pln"></span>> <span class="pln"><meta http 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 class="pln"><title><span class="pln">time/<span class="pln">title</span></span></title></span>> <span class="pun"><span class="pln">head</span></span>> <span class="pln"></span>> <span class="pln"><br class="pun">/</span>> <span class="pln"><script></script></span>> <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="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="pun"><span class="pln">body</span></span>> <span class="pun"><span class="pln">html</span></span>> 运行结果如下: <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="lit">1424069168643<span class="pln"> JS<span class="pun">获得毫秒时间戳</span></span></span> 可以看出,第三个时间戳数值稍微比前两个大,这是代码运行所消耗的时间,是正常的.

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/971768.htmlTechArticlePHP获取毫秒时间戳,php获取毫秒 我们知道,PHP中time()函数获取的时间戳,其单位是秒. 但是,前端JS获取的时间戳,单位是毫秒.那么,在实际应用...
Verwandte Etiketten:
php
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage