Answers.Body: <p>I believe I have figured it out, please correct me so I can update the answer if I am too far off. </p>
  
  <p><strong>TL;DR:</strong></p>
  
  <p>I am using a <em>date</em> picker (no time) on a front-end form. The user chooses a date and it comes across as <code>09/27/2015</code>. Here is how I converted that string to a dateTime object that I could set as post &amp; expiry date for a newly created entry.</p>
  
  <p><strong>NOTE</strong>: I have not included any validation at this point which should be carefully considered. </p>
  
  <p><strong>MyPlugin.php</strong></p>
  
  <pre><code>// Get the current time zone from the system.
  date_default_timezone_set(craft()-&gt;timezone);
  
  // Get the current time
  $time = date('H:i:s');
  
  // Create the post dateTime
  $postDate = date('Y-m-d', strtotime(craft()-&gt;request-&gt;getPost('postDate')));
  $postDateTime = $postDate . ' ' . $time;
  
  // Create the expiry dateTime.
  $expireDate = date('Y-m-d', strtotime(craft()-&gt;request-&gt;getPost('expiryDate')));
  $expireDateTime = $expireDate . ' ' . $time;
  
  // Create the DateTime object to send to Craft.
  $postDate = new \DateTime($postDateTime);
  $expiryDate = new \DateTime($expireDateTime);
  
  // Put it all together.
  $entry = new EntryModel();
  
  $entry-&gt;sectionId = 3;
  $entry-&gt;typeId = 1;
  $entry-&gt;enabled = true;
  $entry-&gt;postDate = $postDate;
  $entry-&gt;expiryDate = $expiryDate;
  
  ...
  
  if(craft()-&gt;entries-&gt;saveEntry($entry)) {
      // Success
  } else {
      // Fail
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-05T20:27:04.267Z
Answers.LastActivityDate: 2015-12-05T20:27:04.267Z
Answers.UserDisplayName: Damon
Body: <p>I am working on a plugin that has a date picker only (no time). I am attempting to set the <code>postDate</code> to the value of whatever date was chosen on the front-end. I realize I need to save in this format: <code>Y-m-d H:i:s</code></p>
  
  <pre><code>$start = craft()-&gt;request-&gt;getPost('startDate'); // 09/27/2015
  
  // I'm not doing this right as it returns nothing.
  $post = DateTime::createFromString($start); 
  
  // Returns a datetime object, but doesn't save.
  $post = new \DateTime($start); 
  
  DateTime Object (
      [date] =&gt; 2015-09-27 00:00:00.000000  // trim the last decimal?
      [timezone_type] =&gt; 3
      [timezone] =&gt; UTC
  )
  </code></pre>
  
  <p>I don't really care what the time is. It could always be midnight, or get the current time from the server. What is the best way to "assemble" the datetime?</p>
  
Tags: <plugin-development><datetime>
Title: How can I set the postDate and expiryDate from a front-end date picker via plugin?
ThreadText: # How can I set the postDate and expiryDate from a front-end date picker via plugin?
  
  (Asked by: username_0 on 2015-12-04)
  
  I am working on a plugin that has a date picker only (no time). I am attempting to set the `postDate` to the value of whatever date was chosen on the front-end. I realize I need to save in this format: `Y-m-d H:i:s`
  
  ```
  $start = craft()->request->getPost('startDate'); // 09/27/2015
  
  // I'm not doing this right as it returns nothing.
  $post = DateTime::createFromString($start); 
  
  // Returns a datetime object, but doesn't save.
  $post = new \DateTime($start); 
  
  DateTime Object (
      [date] => 2015-09-27 00:00:00.000000  // trim the last decimal?
      [timezone_type] => 3
      [timezone] => UTC
  )
  
  ```
  
  I don't really care what the time is. It could always be midnight, or get the current time from the server. What is the best way to "assemble" the datetime?
  
  **Tags:** plugin-development, datetime
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2015-12-05. Score: 5 | Accepted answer
  
  I believe I have figured it out, please correct me so I can update the answer if I am too far off.
  
  **TL;DR:**
  
  I am using a *date* picker (no time) on a front-end form. The user chooses a date and it comes across as `09/27/2015`. Here is how I converted that string to a dateTime object that I could set as post & expiry date for a newly created entry.
  
  **NOTE**: I have not included any validation at this point which should be carefully considered.
  
  **MyPlugin.php**
  
  ```
  // Get the current time zone from the system.
  date_default_timezone_set(craft()->timezone);
  
  // Get the current time
  $time = date('H:i:s');
  
  // Create the post dateTime
  $postDate = date('Y-m-d', strtotime(craft()->request->getPost('postDate')));
  $postDateTime = $postDate . ' ' . $time;
  
  // Create the expiry dateTime.
  $expireDate = date('Y-m-d', strtotime(craft()->request->getPost('expiryDate')));
  $expireDateTime = $expireDate . ' ' . $time;
  
  // Create the DateTime object to send to Craft.
  $postDate = new \DateTime($postDateTime);
  $expiryDate = new \DateTime($expireDateTime);
  
  // Put it all together.
  $entry = new EntryModel();
  
  $entry->sectionId = 3;
  $entry->typeId = 1;
  $entry->enabled = true;
  $entry->postDate = $postDate;
  $entry->expiryDate = $expiryDate;
  
  ...
  
  if(craft()->entries->saveEntry($entry)) {
      // Success
  } else {
      // Fail
  }
  
  ```
  
  
  ---
  
  