1

I try to use "DrupalDateTime" class in my theme to show native date and time in custom format based on special TimeZone in my nodes.
According to this article: New Datetime API

Drupal core and contributed modules should use the DrupalDateTime class in preference to the PHP DateTime class or to functions like date(), gmdate(), strtotime(), etc.

the class incorporates the IntlDateFormatter into its format() method, if the IntlDateFormatter is available, and otherwise falls back to wrapping formatted output with Drupal's translation system. The IntlDateFormatter requires additional information, which can be passed in a settings array.

And then I add below code to MyThmeName.theme:
Note: Be sure to add use Drupal\Core\Datetime\DrupalDateTime; at the top of any files that use this class.

function MyThemeName_preprocess_node(&$variables){
  $node_date = $variables['node']->getCreatedTime();
  $date = new DrupalDateTime($node_date, 'UTC');
  $date->setTimezone(new \DateTimeZone('Asia/Tehran'));
  $variables['date'] = $date->format('Y/m/d'); }

I'm sure PHP intl extension was exist! I cleared comment before this line in php.ini file:

;extension=php_intl.dll

I get below error:

Exception: DateTime object not set. in Drupal\Component\Datetime\DateTimePlus->__call() (line 308 of core\lib\Drupal\Component\Datetime\DateTimePlus.php).

Update:

I'm following kiamlaluno solution carefully but the result not changed and date is shown in the English language yet and howsoever I try I can't get an appropriate result to show date and time in my native calendar, where is the problem, is there other parameters that should be set.

if (isset($variables['node'])) {
  $date = DrupalDateTime::createFromTimestamp($variables['node']->getCreatedTime(), new \DateTimeZone('Asia/Tehran'), ['langcode' => 'fa']);
  $variables['date'] = $date->format('Y/m/d');
}

Thanks for any help.

  • Are you passing $variables by reference? Can you paste the whole function? Are you printing it in the template file? – Kevin Mar 27 '16 at 14:00
  • I just asked for those details. – Kevin Mar 27 '16 at 23:21
  • 1
    If you can't provide details, people can't help you @MojtabaReyhani. We don't need the whole function, just a small, self-contained example that demonstrates the problem. The less information you provide, the less people can help you (it's why your questions aren't being answered at the moment; people want to help, but you're not giving them the chance to) – Clive Mar 30 '16 at 9:24
  • salam dadash, what you mean "date is shown in english language"? language? you want see results in Jalalian ( shamsiye khodemon) ? plz clarify what you want to we can help you. – Yusef Mohamadi Oct 8 '16 at 11:05
  • I see,So what you want? you want see node published date in Shamsi? – Yusef Mohamadi Oct 8 '16 at 11:25
2

The code you are showing is wrong because DrupalDateTime::__construct() is expecting a string, while you are passing an integer, which is what Node::getCreatedTime() returns.

Instead of DrupalDateTime::__construct(), you should use DrupalDateTime::createFromTimestamp().

$date = DrupalDateTime::createFromTimestamp($variables['node']->getCreatedTime(), 'UTC');

Since the static method also accept a \DateTimeZone object as second parameter, your code could be similar to the following one.

if (isset($variables['node'])) {
  $date = DrupalDateTime::createFromTimestamp($variables['node']->getCreatedTime(), new \DateTimeZone('Asia/Tehran'));
  $variables['date'] = $date->format('Y/m/d');
}

Since you seem interested to get strings in a specific language, you should also set the language code.

if (isset($variables['node'])) {
  $date = DrupalDateTime::createFromTimestamp($variables['node']->getCreatedTime(), new \DateTimeZone('Asia/Tehran'), ['langcode' => 'the language code for the language you need']);
  $variables['date'] = $date->format('Y/m/d');
}
  • I added more details. This is all I could find. – kiamlaluno Mar 30 '16 at 9:02
1

if you want to retrun date in Shamsi(jalalian) try to convert it with

 if (isset($variables['node'])) {
 $timestamp = strtotime($variables['node']->getCreatedTime()); 
 $format = 'Y/m/d';
 $timezone = date_default_timezone_get();
 $date = new \IntlDateFormatter("fa_IR@calendar=persian", \IntlDateFormatter::FULL, \IntlDateFormatter::SHORT, $timezone, \IntlDateFormatter::TRADITIONAL);
 $date->setPattern($format);
 $variables['jdate']  =  $date->format(intval($timestamp));
}
  • Thank you so much, This is good solution but not impelement DrupalDateTime class. – Mojtaba Reyhani Oct 8 '16 at 11:52
  • @MojtabaReyhani you're welcome, I asked you I can suggest a way to convert node published date to shamsi with shamsi month name and ... . be win – Yusef Mohamadi Oct 8 '16 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.