Dates.php in Bamboo Twig 8
File
src/TwigExtension/Dates.php
View source
<?php
namespace Drupal\bamboo_twig\TwigExtension;
use Drupal\Core\Datetime\DateFormatter;
class Dates extends \Twig_Extension {
private $dateFormatter;
public function __construct(DateFormatter $dateFormatter) {
$this->dateFormatter = $dateFormatter;
}
public function getFilters() {
return [
new \Twig_SimpleFilter('format_date_i18n', [
$this,
'dateFormati18n',
]),
];
}
public function getName() {
return 'bamboo_twig.twig.dates';
}
public function dateFormati18n($date, $format = 'Y-m-d') {
if (is_a($date, 'Drupal\\Core\\Datetime\\DrupalDateTime') || is_a($date, 'DateTime')) {
$timestmap = $date
->getTimestamp();
}
elseif (\DateTime::createFromFormat('Y-m-d', $date)) {
$timestmap = strtotime($date);
}
else {
$timestmap = $date;
}
try {
$date_format = new \DateTime('@' . $timestmap);
$timestmap = $date_format
->getTimestamp();
} catch (\Exception $e) {
return NULL;
}
return $this->dateFormatter
->format($timestmap, "custom", $format);
}
}
Classes
Name |
Description |
Dates |
Provides a 'Dates' Twig Extensions. |