title: How to create DateTime object from string in symfony2/php
body: <p>In a DB table I have several fields with <code>datetime</code> as field type. So I need to persist data only as date time object.</p>
  
  <p>From a form I get date time as string like</p>
  
  <pre><code>2012-10-05 17:45:54
  </code></pre>
  
  <p>Now when ever I persist my entity I get following error:</p>
  
  <blockquote>
    <p>Fatal error: Call to a member function format() on a non-object in
    ..\DateTimeType.php on line 44</p>
  </blockquote>
  
  <p>I tried with</p>
  
  <pre><code>$protocol-&gt;setStartedAt(strtotime($post['started_at']));
  </code></pre>
  
  <p>or</p>
  
  <pre><code>$from = \DateTime::createFromFormat('yy-mm-dd hh:mm:ss', $post['started_at']);
  $protocol-&gt;setStartedAt($from);
  </code></pre>
  
  <p>or just</p>
  
  <pre><code>$from = new \DateTime($post['started_at']);
  $protocol-&gt;setStartedAt($from);
  </code></pre>
  
  <p>The last code works but it does not uses the timestamp passed as arguement but just gets the current time.</p>
  
  <p>Any ideas?</p>