1

I try to use the DrupalDateTime class in my theme to show native date and time in custom format based on special TimeZone in my nodes.

According to New Datetime API DrupalDateTime should be used instead of the PHP DateTime class or to functions like date(), gmdate(), or strtotime().

I am using the following code in a theme.

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 get this error

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

6
  • Are you passing $variables by reference? Can you paste the whole function? Are you printing it in the template file?
    – Kevin
    Commented Mar 27, 2016 at 14:00
  • I just asked for those details.
    – Kevin
    Commented Mar 27, 2016 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
    Commented Mar 30, 2016 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.
    – Yuseferi
    Commented Oct 8, 2016 at 11:05
  • I see,So what you want? you want see node published date in Shamsi?
    – Yuseferi
    Commented Oct 8, 2016 at 11:25

2 Answers 2

3

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');
}
0
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));
}
2
  • Thank you so much, This is good solution but not impelement DrupalDateTime class. Commented Oct 8, 2016 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
    – Yuseferi
    Commented Oct 8, 2016 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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