uTools-Manuals/docs/php/microtime.html
2019-04-08 23:22:26 +08:00

57 lines
4.7 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>返回当前 Unix 时间戳和微秒数</title>
</head>
<body class="docs"><div id="layout">
<div id="layout-content"><div id="function.microtime" class="refentry">
<div class="refnamediv">
<h1 class="refname">microtime</h1>
<p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">microtime</span> &mdash; <span class="dc-title">返回当前 Unix 时间戳和微秒数</span></p>
</div>
<div class="refsect1 unknown-seealsp" id="refsect1-function.microtime-unknown-seealsp">
<h3 class="title">说明</h3>
<div class="methodsynopsis dc-description">
<span class="methodname"><strong>microtime</strong></span>
([ <span class="methodparam"><span class="type">bool</span> <code class="parameter">$get_as_float</code></span>
] ) : <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span></div>
<p class="para rdfs-comment">
<span class="function"><strong>microtime()</strong></span> 当前 Unix
时间戳以及微秒数。本函数仅在支持 gettimeofday()
系统调用的操作系统下可用。
</p>
<p class="para">
如果调用时不带可选参数,本函数以 &quot;msec sec&quot;
的格式返回一个字符串,其中 sec 是自 Unix 纪元0:00:00 January 1,
1970 GMT起到现在的秒数msec 是微秒部分。字符串的两部分都是以秒为单位返回的。
</p>
<p class="para">
如果给出了 <code class="parameter">get_as_float</code> 参数并且其值等价于
<strong><code>TRUE</code></strong><span class="function"><strong>microtime()</strong></span> 将返回一个浮点数。
</p>
<blockquote class="note"><p><strong class="note">Note</strong>:
<span class="simpara">
<code class="parameter">get_as_float</code> 参数是 PHP 5.0.0 新加的。
</span>
</p></blockquote>
<p class="para">
<div class="example" id="example-2726">
<p><strong>Example #1 用 <span class="function"><strong>microtime()</strong></span> 对脚本的运行计时</strong></p>
<div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/**<br />&nbsp;*&nbsp;Simple&nbsp;function&nbsp;to&nbsp;replicate&nbsp;PHP&nbsp;5&nbsp;behaviour<br />&nbsp;*/<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">microtime_float</span><span style="color: #007700">()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;list(</span><span style="color: #0000BB">$usec</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$sec</span><span style="color: #007700">)&nbsp;=&nbsp;</span><span style="color: #0000BB">explode</span><span style="color: #007700">(</span><span style="color: #DD0000">"&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">microtime</span><span style="color: #007700">());<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;((float)</span><span style="color: #0000BB">$usec&nbsp;</span><span style="color: #007700">+&nbsp;(float)</span><span style="color: #0000BB">$sec</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$time_start&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">microtime_float</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">//&nbsp;Sleep&nbsp;for&nbsp;a&nbsp;while<br /></span><span style="color: #0000BB">usleep</span><span style="color: #007700">(</span><span style="color: #0000BB">100</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$time_end&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">microtime_float</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$time&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$time_end&nbsp;</span><span style="color: #007700">-&nbsp;</span><span style="color: #0000BB">$time_start</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Did&nbsp;nothing&nbsp;in&nbsp;</span><span style="color: #0000BB">$time</span><span style="color: #DD0000">&nbsp;seconds\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
</div>
</div>
</p>
<p class="para">
参见 <span class="function"><a href="time.html" class="function">time()</a></span>
</p>
</div>
</div></div></div></body></html>