<!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>Return current Unix timestamp with microseconds</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.localtime.html">localtime</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.mktime.html">mktime</a></div> <div class="up"><a href="ref.datetime.html">Date/Time Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.microtime" class="refentry"> <div class="refnamediv"> <h1 class="refname">microtime</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">microtime</span> — <span class="dc-title">Return current Unix timestamp with microseconds</span></p> </div> <div class="refsect1 description" id="refsect1-function.microtime-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>microtime</strong></span> ([ <span class="methodparam"><span class="type">bool</span> <code class="parameter">$get_as_float</code><span class="initializer"> = false</span></span> ] )</div> <p class="para rdfs-comment"> <span class="function"><strong>microtime()</strong></span> returns the current Unix timestamp with microseconds. This function is only available on operating systems that support the gettimeofday() system call. </p> </div> <div class="refsect1 parameters" id="refsect1-function.microtime-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">get_as_float</code></em></span> <dd> <p class="para"> If used and set to <strong><code>TRUE</code></strong>, <span class="function"><strong>microtime()</strong></span> will return a <span class="type"><a href="language.types.float.html" class="type float">float</a></span> instead of a <span class="type"><a href="language.types.string.html" class="type string">string</a></span>, as described in the return values section below. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.microtime-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> By default, <span class="function"><strong>microtime()</strong></span> returns a <span class="type"><a href="language.types.string.html" class="type string">string</a></span> in the form "msec sec", where <em>sec</em> is the number of seconds since the Unix epoch (0:00:00 January 1,1970 GMT), and <em>msec</em> measures microseconds that have elapsed since <em>sec</em> and is also expressed in seconds. </p> <p class="para"> If <em><code class="parameter">get_as_float</code></em> is set to <strong><code>TRUE</code></strong>, then <span class="function"><strong>microtime()</strong></span> returns a <span class="type"><a href="language.types.float.html" class="type float">float</a></span>, which represents the current time in seconds since the Unix epoch accurate to the nearest microsecond. </p> </div> <div class="refsect1 changelog" id="refsect1-function.microtime-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.0.0</td> <td> The <em><code class="parameter">get_as_float</code></em> parameter was added. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.microtime-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-2312"> <p><strong>Example #1 Timing script execution with <span class="function"><strong>microtime()</strong></span></strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">/**<br /> * Simple function to replicate PHP 5 behaviour<br /> */<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">microtime_float</span><span style="color: #007700">()<br />{<br /> list(</span><span style="color: #0000BB">$usec</span><span style="color: #007700">, </span><span style="color: #0000BB">$sec</span><span style="color: #007700">) = </span><span style="color: #0000BB">explode</span><span style="color: #007700">(</span><span style="color: #DD0000">" "</span><span style="color: #007700">, </span><span style="color: #0000BB">microtime</span><span style="color: #007700">());<br /> return ((float)</span><span style="color: #0000BB">$usec </span><span style="color: #007700">+ (float)</span><span style="color: #0000BB">$sec</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$time_start </span><span style="color: #007700">= </span><span style="color: #0000BB">microtime_float</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// Sleep for a 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 </span><span style="color: #007700">= </span><span style="color: #0000BB">microtime_float</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$time </span><span style="color: #007700">= </span><span style="color: #0000BB">$time_end </span><span style="color: #007700">- </span><span style="color: #0000BB">$time_start</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #DD0000">"Did nothing in </span><span style="color: #0000BB">$time</span><span style="color: #DD0000"> seconds\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <div class="example" id="example-2313"> <p><strong>Example #2 Timing script execution in PHP 5</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$time_start </span><span style="color: #007700">= </span><span style="color: #0000BB">microtime</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Sleep for a 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 </span><span style="color: #007700">= </span><span style="color: #0000BB">microtime</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$time </span><span style="color: #007700">= </span><span style="color: #0000BB">$time_end </span><span style="color: #007700">- </span><span style="color: #0000BB">$time_start</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #DD0000">"Did nothing in </span><span style="color: #0000BB">$time</span><span style="color: #DD0000"> seconds\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <div class="example" id="example-2314"> <p><strong>Example #3 <span class="function"><strong>microtime()</strong></span> and <em>REQUEST_TIME_FLOAT</em> (as of PHP 5.4.0)</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// Randomize sleeping time<br /></span><span style="color: #0000BB">usleep</span><span style="color: #007700">(</span><span style="color: #0000BB">mt_rand</span><span style="color: #007700">(</span><span style="color: #0000BB">100</span><span style="color: #007700">, </span><span style="color: #0000BB">10000</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// As of PHP 5.4.0, REQUEST_TIME_FLOAT is available in the $_SERVER superglobal array.<br />// It contains the timestamp of the start of the request with microsecond precision.<br /></span><span style="color: #0000BB">$time </span><span style="color: #007700">= </span><span style="color: #0000BB">microtime</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">) - </span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">"REQUEST_TIME_FLOAT"</span><span style="color: #007700">];<br /><br />echo </span><span style="color: #DD0000">"Did nothing in </span><span style="color: #0000BB">$time</span><span style="color: #DD0000"> seconds\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.microtime-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.time.html" class="function" rel="rdfs-seeAlso">time()</a> - Return current Unix timestamp</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.localtime.html">localtime</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.mktime.html">mktime</a></div> <div class="up"><a href="ref.datetime.html">Date/Time Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>