function TimeInterface::getRequestTime
Same name in other branches
- 9 core/lib/Drupal/Component/Datetime/TimeInterface.php \Drupal\Component\Datetime\TimeInterface::getRequestTime()
- 8.9.x core/lib/Drupal/Component/Datetime/TimeInterface.php \Drupal\Component\Datetime\TimeInterface::getRequestTime()
- 11.x core/lib/Drupal/Component/Datetime/TimeInterface.php \Drupal\Component\Datetime\TimeInterface::getRequestTime()
Returns the timestamp for the current request.
This method should be used to obtain the current system time 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.
This method can replace instances of
$request_time = $_SERVER['REQUEST_TIME'];
$request_time = $requestStack->getCurrentRequest()->server
->get('REQUEST_TIME');
$request_time = $request->server
->get('REQUEST_TIME');
and most instances of
$time = time();
with
$request_time = \Drupal::time()->getRequestTime();
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
int A Unix timestamp.
See also
\Drupal\Component\Datetime\TimeInterface::getRequestMicroTime()
\Drupal\Component\Datetime\TimeInterface::getCurrentTime()
\Drupal\Component\Datetime\TimeInterface::getCurrentMicroTime()
File
-
core/
lib/ Drupal/ Component/ Datetime/ TimeInterface.php, line 46
Class
- TimeInterface
- Defines an interface for obtaining system time.
Namespace
Drupal\Component\DatetimeCode
public function getRequestTime();
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.