title: Call to a member function format() on a non-object when dealing with date/time
body: <p>I'm trying to save some data to my database and need to format a date/time before the save.</p>
  
  <p>my code is as follows:</p>
  
  <pre><code>public static function toSqlDate($date, $formatResult = true)
      {
          if (!$date)
          {
              return null;
          }
  
          $dateTime = DateTime::createFromFormat(DEFAULT_DATETIME_FORMAT, $date, new DateTimeZone(DEFAULT_TIMEZONE));
  
          return $formatResult ? $dateTime-&gt;format('Y-m-d') : $dateTime;
      }
  </code></pre>
  
  <p>When I try to save I get the following error in the debugger. the return line is the line highlighted as having the error.</p>
  
  <blockquote>
    <p>Symfony \ Component \ Debug \ Exception \ FatalErrorException<br>
    Call to a member function format() on a non-object</p>
  </blockquote>
  
  <p>I'm not really sure how to even attempt to fix this. Is there a way I can output what each of the values being pulled in are before it throws the error? I'm trying to figure out what $dateTime is not being returned as an object.</p>
  
  <p>any help would greatly be appreciated.</p>
  
  <p>Some additional information:</p>
  
  <p>I have created a utility/alias pointing the the function above and in my model repository I access it via the following.</p>
  
  <pre><code>public function save($publicId, $data)
  { 
  ...
  
      $form-&gt;date = Utils::toSqlDate($data['date']);
      ...
      $form-&gt;save();
  
  ...
  }
  </code></pre>
  
  <p>and I have the following field in my view</p>
  
  <pre><code>{{ Former::text('date')-&gt;addClass('form-control form-control-inline input-medium default-date-picker')-&gt;data_bind("datePicker: date, valueUpdate: 'afterkeydown'") }}
  </code></pre>