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();
}

 

 

 

 

Comments

Add new comment

Restricted HTML

Search

Page Facebook

Become a patron

If you need some help or you search a Drupal freelancer don't hesitate to contact us.

 

Contact Us

All the content is FREE but I still need your help

 

Become a patreon