3 namespace Drupal\Core\Datetime;
5 use Drupal\Core\Cache\CacheableDependencyInterface;
6 use Drupal\Core\Cache\UnchangingCacheableDependencyTrait;
7 use Drupal\Core\Render\RenderableInterface;
10 * Contains a formatted time difference.
12 class FormattedDateDiff implements RenderableInterface, CacheableDependencyInterface {
14 use UnchangingCacheableDependencyTrait;
17 * The actual formatted time difference.
24 * The maximum time in seconds that this string may be cached.
26 * Let's say the time difference is 1 day 1 hour. In this case, we can cache
27 * it until now + 1 hour, so maxAge is 3600 seconds.
34 * Creates a new FormattedDateDiff instance.
36 * @param string $string
37 * The formatted time difference.
39 * The maximum time in seconds that this string may be cached.
41 public function __construct($string, $max_age) {
42 $this->string = $string;
43 $this->maxAge = $max_age;
49 public function getString() {
56 public function getCacheMaxAge() {
61 * The maximum age for which this object may be cached.
64 * The maximum time in seconds that this object may be cached.
66 * @deprecated in Drupal 8.1.9 and will be removed before Drupal 9.0.0. Use
67 * \Drupal\Core\Datetime\FormattedDateDiff::getCacheMaxAge() instead.
69 public function getMaxAge() {
70 return $this->getCacheMaxAge();
76 public function toRenderable() {
78 '#markup' => $this->string,
80 'max-age' => $this->maxAge,