Why is my Datetime field not saved? I use the following code to create a node.

    $datetime = "2016-08-01 12:30:00";
    $values = array(
      'title' =&gt; $data['title'],
      'body' =&gt; $data['body'],
      'field_client' =&gt; $uid,
      'field_appointment_date_time' =&gt; $datetime,
      'type' =&gt; 'appointment'
   );

  $appointment = Node::create($values);
  $appointment-&gt;save();


The problem is that field_appointment_date_time (a Datetime field) is not been saved.

Why does this happen?
The above code is correct, date time was saved into DB.
the problem is frontend.

frontend expected this format:

"2016-08-01T12:30:00", need a "T" as separator.


here is how to put that T as separator,

\DateTime::format("Y-m-d\Th:i:s"), need a escape character in-front of T.


2016-01-22 update:

By the way, I have found out in Drupal 8 , correct way to store datetime is follow:

1&gt; first , find out current user timezone, \Drupal::currentUser()-&gt;getTimezone()

2&gt; $given = new \Drupal\Core\Datetime\DrupalDateTime("2016-08-01T12:30:00",$user-&gt;getTimezone());

3&gt; $given-&gt;setTimezone(new \DateTimeZone("UTC"));

4&gt; then get back formated datetime string $given-&gt;format("Y-m-d\Th:i:s");