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