Skip to main content

Drupal 8: How to get the local date from DrupalDateTime

This is the way you can get the local Date for your country. So for my case i want to get "Δευτέρα 15 Φεβρουαρίου 2021" and not "Monday 15 February 2021" which is the local date for Greece. In order to use the Drupal Date Time in your controller for example you have to add the namespace on the top of your controller class like this

use Drupal\Core\Datetime\DrupalDateTime;

and then:

class CustomController extends ControllerBase {
  /**
   * {@inheritdoc}.
   */
  public function index(NodeInterface $node) {
    $drupalDate = new DrupalDateTime(date('Y-m-d H:i',$date['date']));  //$date['date'] is a in unixtimestamp
    $drupalDate->format('l'); // Will print "Δευτέρα" for Greece
    $drupalDate->format('d F')); // Will print 15 Φεβρουάριος for Greece etc
 }

 

php date

Latest Articles

Using drupal/core-composer-scaffold to prevent rewriting .htaccess or robots.txt file admin_pixelthi… Sun, 12/01/2024 - 16:49 Using drupal/core-composer-scaffold to prevent rewriting .htaccess or robots.txt file

Using drupal/core-composer-scaffold to prevent rewriting .htaccess or robots.txt file

composer
Controlling ckeditor styling options with hook_editor_js_settings_alter admin_pixelthi… Thu, 05/16/2024 - 10:49 Controlling ckeditor styling options with hook_editor_js_settings_alter

Controlling ckeditor styling options with hook_editor_js_settings_alter

php hook hook_editor_js_settings_alter hook_field_widget_single_[WIDGET_TYPE]_form_alter
Upgrading Drupal 9 Modules for Drupal 10: A Simple Guide admin_pixelthi… Tue, 09/05/2023 - 14:18 Upgrading Drupal 9 Modules for Drupal 10: A Simple Guide

Upgrading Drupal 9 Modules for Drupal 10: A Simple Guide

composer drupal9 drupal10