Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. TimeInterface.php

function TimeInterface::getRequestMicroTime

Returns the timestamp for the current request with microsecond precision.

This method should be used to obtain the current system time, with microsecond precision, at the start of the request. It will be the same value for the life of the request (even for long execution times).

If the request is not available it will fallback to the current system time with microsecond precision.

This method can replace instances of

$request_time_float = $_SERVER['REQUEST_TIME_FLOAT'];
$request_time_float = $requestStack->getCurrentRequest()->server
    ->get('REQUEST_TIME_FLOAT');
$request_time_float = $request->server
    ->get('REQUEST_TIME_FLOAT');

and many instances of

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

with

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

or the equivalent using the injected service.

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::getCurrentTime()

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

1 method overrides TimeInterface::getRequestMicroTime()
Time::getRequestMicroTime in core/lib/Drupal/Component/Datetime/Time.php
Returns the timestamp for the current request with microsecond precision.

File

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

Class

TimeInterface
Defines an interface for obtaining system time.

Namespace

Drupal\Component\Datetime

Code

public function getRequestMicroTime();

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal