2010-03-28 22:10:10 +00:00
|
|
|
<?xml version="1.0" encoding="utf-8"?>
|
2009-07-11 06:46:52 +00:00
|
|
|
<!-- $Revision$ -->
|
2007-06-20 22:25:43 +00:00
|
|
|
<refentry xml:id="function.microtime" xmlns="http://docbook.org/ns/docbook">
|
2005-04-24 23:50:58 +00:00
|
|
|
<refnamediv>
|
|
|
|
<refname>microtime</refname>
|
|
|
|
<refpurpose>Return current Unix timestamp with microseconds</refpurpose>
|
|
|
|
</refnamediv>
|
2006-07-11 07:05:04 +00:00
|
|
|
|
|
|
|
<refsect1 role="description">
|
|
|
|
&reftitle.description;
|
2005-04-24 23:50:58 +00:00
|
|
|
<methodsynopsis>
|
|
|
|
<type>mixed</type><methodname>microtime</methodname>
|
2020-07-27 11:47:54 +00:00
|
|
|
<methodparam choice="opt"><type>bool</type><parameter>getAsFloat</parameter><initializer>&false;</initializer></methodparam>
|
2005-04-24 23:50:58 +00:00
|
|
|
</methodsynopsis>
|
|
|
|
<para>
|
|
|
|
<function>microtime</function> returns the current Unix timestamp with
|
|
|
|
microseconds. This function is only available on operating systems that
|
|
|
|
support the gettimeofday() system call.
|
|
|
|
</para>
|
2020-02-20 08:14:20 +00:00
|
|
|
<para>
|
|
|
|
For performance measurements, using <function>hrtime</function> is recommended.
|
|
|
|
</para>
|
2006-07-11 07:05:04 +00:00
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 role="parameters">
|
|
|
|
&reftitle.parameters;
|
2005-04-24 23:50:58 +00:00
|
|
|
<para>
|
2006-07-11 07:05:04 +00:00
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
2020-07-27 11:47:54 +00:00
|
|
|
<term><parameter>getAsFloat</parameter></term>
|
2006-07-11 07:05:04 +00:00
|
|
|
<listitem>
|
|
|
|
<para>
|
2011-02-08 10:27:42 +00:00
|
|
|
If used and set to &true;, <function>microtime</function> will return a
|
|
|
|
<type>float</type> instead of a <type>string</type>, as described in
|
|
|
|
the return values section below.
|
2006-07-11 07:05:04 +00:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
</variablelist>
|
2005-04-24 23:50:58 +00:00
|
|
|
</para>
|
2011-02-08 10:27:42 +00:00
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 role="returnvalues">
|
|
|
|
&reftitle.returnvalues;
|
|
|
|
<para>
|
|
|
|
By default, <function>microtime</function> returns a <type>string</type> in
|
2013-10-25 15:58:40 +00:00
|
|
|
the form "msec sec", where <literal>sec</literal> is the number of seconds
|
|
|
|
since the Unix epoch (0:00:00 January 1,1970 GMT), and <literal>msec</literal>
|
|
|
|
measures microseconds that have elapsed since <literal>sec</literal>
|
|
|
|
and is also expressed in seconds.
|
2011-02-08 10:27:42 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
2020-07-27 11:47:54 +00:00
|
|
|
If <parameter>getAsFloat</parameter> is set to &true;, then
|
2011-02-08 10:27:42 +00:00
|
|
|
<function>microtime</function> returns a <type>float</type>, which
|
|
|
|
represents the current time in seconds since the Unix epoch accurate to the
|
|
|
|
nearest microsecond.
|
|
|
|
</para>
|
2006-07-11 07:05:04 +00:00
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 role="examples">
|
|
|
|
&reftitle.examples;
|
2005-04-24 23:50:58 +00:00
|
|
|
<para>
|
|
|
|
<example>
|
|
|
|
<title>Timing script execution with <function>microtime</function></title>
|
|
|
|
<programlisting role="php">
|
2002-04-15 00:12:54 +00:00
|
|
|
<![CDATA[
|
2003-03-21 15:46:14 +00:00
|
|
|
<?php
|
2004-07-30 14:50:51 +00:00
|
|
|
/**
|
2004-08-26 05:40:30 +00:00
|
|
|
* Simple function to replicate PHP 5 behaviour
|
2004-07-30 14:50:51 +00:00
|
|
|
*/
|
2005-04-24 23:50:58 +00:00
|
|
|
function microtime_float()
|
|
|
|
{
|
|
|
|
list($usec, $sec) = explode(" ", microtime());
|
|
|
|
return ((float)$usec + (float)$sec);
|
|
|
|
}
|
2002-04-15 00:12:54 +00:00
|
|
|
|
2004-07-30 14:50:51 +00:00
|
|
|
$time_start = microtime_float();
|
|
|
|
|
|
|
|
// Sleep for a while
|
|
|
|
usleep(100);
|
2002-04-15 00:12:54 +00:00
|
|
|
|
2004-07-30 14:50:51 +00:00
|
|
|
$time_end = microtime_float();
|
2002-04-15 00:12:54 +00:00
|
|
|
$time = $time_end - $time_start;
|
|
|
|
|
2003-12-18 17:46:56 +00:00
|
|
|
echo "Did nothing in $time seconds\n";
|
2004-07-30 14:50:51 +00:00
|
|
|
?>
|
|
|
|
]]>
|
2005-04-24 23:50:58 +00:00
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
<example>
|
|
|
|
<title>Timing script execution in PHP 5</title>
|
|
|
|
<programlisting role="php">
|
2004-07-30 14:50:51 +00:00
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
$time_start = microtime(true);
|
2003-12-18 17:46:56 +00:00
|
|
|
|
2004-07-30 14:50:51 +00:00
|
|
|
// Sleep for a while
|
|
|
|
usleep(100);
|
2003-12-18 17:46:56 +00:00
|
|
|
|
2004-07-30 14:50:51 +00:00
|
|
|
$time_end = microtime(true);
|
2003-12-18 17:46:56 +00:00
|
|
|
$time = $time_end - $time_start;
|
|
|
|
|
2012-11-12 22:34:55 +00:00
|
|
|
echo "Did nothing in $time seconds\n";
|
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
<example>
|
|
|
|
<title><function>microtime</function> and <literal>REQUEST_TIME_FLOAT</literal> (as of PHP 5.4.0)</title>
|
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
// Randomize sleeping time
|
|
|
|
usleep(mt_rand(100, 10000));
|
|
|
|
|
|
|
|
// As of PHP 5.4.0, REQUEST_TIME_FLOAT is available in the $_SERVER superglobal array.
|
|
|
|
// It contains the timestamp of the start of the request with microsecond precision.
|
|
|
|
$time = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];
|
|
|
|
|
2003-12-18 17:46:56 +00:00
|
|
|
echo "Did nothing in $time seconds\n";
|
2003-03-21 15:46:14 +00:00
|
|
|
?>
|
2002-04-15 00:12:54 +00:00
|
|
|
]]>
|
2005-04-24 23:50:58 +00:00
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
</para>
|
2006-07-11 07:05:04 +00:00
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 role="seealso">
|
|
|
|
&reftitle.seealso;
|
2005-04-24 23:50:58 +00:00
|
|
|
<para>
|
2006-07-11 07:05:04 +00:00
|
|
|
<simplelist>
|
|
|
|
<member><function>time</function></member>
|
2020-02-20 08:14:20 +00:00
|
|
|
<member><function>hrtime</function></member>
|
2006-07-11 07:05:04 +00:00
|
|
|
</simplelist>
|
2005-04-24 23:50:58 +00:00
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
2002-04-15 00:12:54 +00:00
|
|
|
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
|
|
Local variables:
|
|
|
|
mode: sgml
|
|
|
|
sgml-omittag:t
|
|
|
|
sgml-shorttag:t
|
|
|
|
sgml-minimize-attributes:nil
|
|
|
|
sgml-always-quote-attributes:t
|
|
|
|
sgml-indent-step:1
|
|
|
|
sgml-indent-data:t
|
|
|
|
indent-tabs-mode:nil
|
|
|
|
sgml-parent-document:nil
|
2009-09-25 07:04:39 +00:00
|
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
2002-04-15 00:12:54 +00:00
|
|
|
sgml-exposed-tags:nil
|
|
|
|
sgml-local-catalogs:nil
|
|
|
|
sgml-local-ecat-files:nil
|
|
|
|
End:
|
|
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
|
|
vim: et tw=78 syn=sgml
|
|
|
|
vi: ts=1 sw=1
|
2004-08-10 16:30:22 +00:00
|
|
|
-->
|