You are here

public function TimeInterface::getCurrentMicroTime in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Component/Datetime/TimeInterface.php \Drupal\Component\Datetime\TimeInterface::getCurrentMicroTime()

Returns the current system time with microsecond precision.

This method should be used to obtain the current system time, with microsecond precision, at the time the method was called.

This method can replace many instances of

$microtime = microtime();
$microtime = microtime(TRUE);

with

$request_time = \Drupal::time()
  ->getCurrentMicroTime();

or the equivalent using the injected service.

This method should only be used when the current system time is actually needed, such as with timers or time interval calculations. If only the time at the start of the request and microsecond precision is needed, use TimeInterface::getRequestMicroTime().

Using the time service, rather than other methods, is especially important when creating tests, which require predictable timestamps.

Return value

float A Unix timestamp with a fractional portion.

See also

\Drupal\Component\Datetime\TimeInterface::getRequestTime()

\Drupal\Component\Datetime\TimeInterface::getRequestMicroTime()

\Drupal\Component\Datetime\TimeInterface::getCurrentTime()

1 method overrides TimeInterface::getCurrentMicroTime()
Time::getCurrentMicroTime in core/lib/Drupal/Component/Datetime/Time.php
Returns the current system time with microsecond precision.

File

core/lib/Drupal/Component/Datetime/TimeInterface.php, line 153

Class

TimeInterface
Defines an interface for obtaining system time.

Namespace

Drupal\Component\Datetime

Code

public function getCurrentMicroTime();