0

I am programatically saving values from an array $customer into a content type. The below code works fine and populates the content type customer_profile except for the field date of birth. This field is of type date and date type is set as date only (not date and time). The value in $customer['dob'] is '19/12/1938'. How do I insert this value?

$node_data['type'] = 'customer_profile';
$node_data['title'] = $customer['First Name'].' '.$customer['Last Name'];
$node_data['field_customer_gender']['value'] = $customer['Gender'];
$node_data['field_date_of_birth']['value'] = $customer['dob'];
$node = Node::create($node_data);
$node->setPublished(TRUE);
$node->save();  
2

2 Answers 2

0

I think correct format is 'yyyy-mm-dd', try to insert '1938-12-19'

1
  • 1
    DateTime::createFromFormat('d/m/Y', '19/12/1938')->format('Y-m-d'); The above works in a normal php script and returns the desired format but when I assign $node_data['field_date_of_birth']['value'] = DateTime::createFromFormat('d/m/Y', '19/12/1938')->format('Y-m-d'); it doesnt work. I dont understand why Commented Nov 15, 2018 at 15:04
0

I successfully converted and inserted the date by

   $node_data['field_date_of_birth']['value'] = Datetime\DrupalDateTime::createFromFormat('d/m/Y', $item['DOB'])->format('Y-m-d')

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.