Skip to main content
Category:

Code snippet that can be used to set programmatically the node created/changed dates in Drupal 8.

this is not work:

$node->created = "01/10/2020";

to resolve this problem just use strtotime ($EnglishDateTime, $time_now) like this:

$node->created = strtotime("01/10/2020");

Example:

<?php

use Drupal\node\Entity\Node;

/**
 * hook_cron()
 * @throws \Drupal\Core\Entity\EntityStorageException
 */
function mymodule_cron(){
  $node = Node::create(['type' => 'article']);
  $node->uid = 1;
  $node->promote = 0;
  $node->sticky = 0;
  $node->title= "hello world teste 4";
  $node->body = "<strong>hello world teste</strong>";
  $node->created = strtotime("01/10/2020");
  $node->changed = strtotime("01/14/2020");
  $node->save();
}

 

 

 

 

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook