You are here

public function FrozenTime::getRequestTime in Lingotek Translation 3.3.x

Same name and namespace in other branches
  1. 8.2 tests/modules/frozenintime/src/FrozenTime.php \Drupal\frozenintime\FrozenTime::getRequestTime()
  2. 4.0.x tests/modules/frozenintime/src/FrozenTime.php \Drupal\frozenintime\FrozenTime::getRequestTime()
  3. 3.0.x tests/modules/frozenintime/src/FrozenTime.php \Drupal\frozenintime\FrozenTime::getRequestTime()
  4. 3.1.x tests/modules/frozenintime/src/FrozenTime.php \Drupal\frozenintime\FrozenTime::getRequestTime()
  5. 3.2.x tests/modules/frozenintime/src/FrozenTime.php \Drupal\frozenintime\FrozenTime::getRequestTime()
  6. 3.4.x tests/modules/frozenintime/src/FrozenTime.php \Drupal\frozenintime\FrozenTime::getRequestTime()
  7. 3.5.x tests/modules/frozenintime/src/FrozenTime.php \Drupal\frozenintime\FrozenTime::getRequestTime()
  8. 3.6.x tests/modules/frozenintime/src/FrozenTime.php \Drupal\frozenintime\FrozenTime::getRequestTime()
  9. 3.7.x tests/modules/frozenintime/src/FrozenTime.php \Drupal\frozenintime\FrozenTime::getRequestTime()
  10. 3.8.x tests/modules/frozenintime/src/FrozenTime.php \Drupal\frozenintime\FrozenTime::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 = 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.

Overrides TimeInterface::getRequestTime

See also

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

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

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

File

tests/modules/frozenintime/src/FrozenTime.php, line 19

Class

FrozenTime
Time implementation were time is frozen

Namespace

Drupal\frozenintime

Code

public function getRequestTime() {
  return static::MY_BIRTHDAY;
}