function TimeInterface::getCurrentTime

Same name in other branches
  1. 8.9.x core/lib/Drupal/Component/Datetime/TimeInterface.php \Drupal\Component\Datetime\TimeInterface::getCurrentTime()
  2. 10 core/lib/Drupal/Component/Datetime/TimeInterface.php \Drupal\Component\Datetime\TimeInterface::getCurrentTime()
  3. 11.x core/lib/Drupal/Component/Datetime/TimeInterface.php \Drupal\Component\Datetime\TimeInterface::getCurrentTime()

Returns the current system time as an integer.

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

This method can replace many instances of

$time = time();

with

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

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 is needed, use TimeInterface::getRequestTime().

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

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

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

1 method overrides TimeInterface::getCurrentTime()
Time::getCurrentTime in core/lib/Drupal/Component/Datetime/Time.php
Returns the current system time as an integer.

File

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

Class

TimeInterface
Defines an interface for obtaining system time.

Namespace

Drupal\Component\Datetime

Code

public function getCurrentTime();

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.