Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh

Code snippet that can be used to to get year and month from date_iso8601 for computed field in drupal 8.

Example with date()

$date = $entity->created->value;
$date = date('Y-m-d H:i:s', $date);


Example get year

$date = $node->getCreatedTime();
$year = date('Y', $date);


Example to get year with date.formatter service:

$date = $node->getCreatedTime();
$year = \Drupal::service('date.formatter')->format($date, 'html_year');


Example to get year with DrupalDateTime class:

$date = $node->getCreatedTime();
$date = DrupalDateTime::createFromTimestamp($date);
$year = $date->format('Y');


Add new comment

Restricted HTML


Page Facebook

Become a patron

If you need some help or you search a Drupal freelancer don't hesitate to contact us.


Contact Us

All the content is FREE but I still need your help


Become a patreon