w3resource

PHP: microtime() function

Description

The microtime() function creates current Unix timestamp with microseconds values.

Version:

(PHP 4 and above)

Syntax:

microtime(get_as_float) 

Parameter:

Name Description Required /
Optional
Type
get_as_float When it set to true then a float (in seconds) is returned instead of a string. Optional Boolean

Return value:

Unix timestamp

Value Type: Mixed*.

*Mixed : Mixed indicates multiple (but not necessarily all) types.

Example:

<?php
echo(microtime());
?>

Output:

0.01583100 1295421766

Note: Since the current time is not static, therefore the output may vary.

View the example in the browser

See also

PHP Function Reference

Previous: localtime
Next: mktime



PHP: Tips of the Day

Getting all defined constants

To get all defined constants including those created by PHP use the get_defined_constants function:

Example:

<?php
$constants = get_defined_constants();
var_dump($constants); // pretty large list

Output:

array(2250) {
  ["E_ERROR"]=>
  int(1)
  ["E_RECOVERABLE_ERROR"]=>
  int(4096)
  ["E_WARNING"]=>
  .....
  .....
  resource(1) of type (stream)
  ["STDOUT"]=>
  resource(2) of type (stream)
  ["STDERR"]=>
  resource(3) of type (stream)
}

To get only those constants that were defined by your app call the function at the beginning and at the end of your script (normally after the bootstrap process):

<?php
$constants = get_defined_constants();
define("HELLO", "hello");
define("WORLD", "world");
$new_constants = get_defined_constants();
$myconstants = array_diff_assoc($new_constants, $constants);
var_export($myconstants);

Output:

array (
  'HELLO' => 'hello',
  'WORLD' => 'world',
)

It's sometimes useful for debugging