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

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

  • 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 – Akhil Mohandas Nov 15 '18 at 15:04

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, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.