class FormattedDateDiff in Drupal 9
Same name and namespace in other branches
- 8 core/lib/Drupal/Core/Datetime/FormattedDateDiff.php \Drupal\Core\Datetime\FormattedDateDiff
Contains a formatted time difference.
Hierarchy
- class \Drupal\Core\Datetime\FormattedDateDiff implements CacheableDependencyInterface, RenderableInterface uses UnchangingCacheableDependencyTrait
Expanded class hierarchy of FormattedDateDiff
1 file declares its use of FormattedDateDiff
- DateTest.php in core/
tests/ Drupal/ Tests/ Core/ Datetime/ DateTest.php
File
- core/
lib/ Drupal/ Core/ Datetime/ FormattedDateDiff.php, line 12
Namespace
Drupal\Core\DatetimeView source
class FormattedDateDiff implements RenderableInterface, CacheableDependencyInterface {
use UnchangingCacheableDependencyTrait;
/**
* The actual formatted time difference.
*
* @var string
*/
protected $string;
/**
* The maximum time in seconds that this string may be cached.
*
* Let's say the time difference is 1 day 1 hour. In this case, we can cache
* it until now + 1 hour, so maxAge is 3600 seconds.
*
* @var int
*/
protected $maxAge;
/**
* Creates a new FormattedDateDiff instance.
*
* @param string $string
* The formatted time difference.
* @param int $max_age
* The maximum time in seconds that this string may be cached.
*/
public function __construct($string, $max_age) {
$this->string = $string;
$this->maxAge = $max_age;
}
/**
* @return string
*/
public function getString() {
return $this->string;
}
/**
* {@inheritdoc}
*/
public function getCacheMaxAge() {
return $this->maxAge;
}
/**
* {@inheritdoc}
*/
public function toRenderable() {
return [
'#markup' => $this->string,
'#cache' => [
'max-age' => $this->maxAge,
],
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FormattedDateDiff:: |
protected | property | The maximum time in seconds that this string may be cached. | |
FormattedDateDiff:: |
protected | property | The actual formatted time difference. | |
FormattedDateDiff:: |
public | function |
The maximum age for which this object may be cached. Overrides UnchangingCacheableDependencyTrait:: |
|
FormattedDateDiff:: |
public | function | ||
FormattedDateDiff:: |
public | function |
Returns a render array representation of the object. Overrides RenderableInterface:: |
|
FormattedDateDiff:: |
public | function | Creates a new FormattedDateDiff instance. | |
UnchangingCacheableDependencyTrait:: |
public | function | 1 | |
UnchangingCacheableDependencyTrait:: |
public | function | 1 |