n How to set programmatically the node created/changed dates in Drupal 8 | CodimTh

Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
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