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. As for the other methods
Instead of DrupalDateTime::__construct()
, you are usingshould use DrupalDateTime::createFromTimestamp()
.
$date = DrupalDateTime::createFromTimestamp($variables['node']->getCreatedTime(), 'UTC');
Since the static method also accept a \DateTimeZone
object as second parameter, I don't see them listed inyour code could be similar to the methods available forfollowing 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 DrupalDateTime
classlanguage 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');
}