<p>After <code>$entry-&gt;setContentFromPost($fieldsLocation);</code> you should be able to add another line to explicitly set your calculated date.</p>

<pre><code>$entry-&gt;setContent(array('fieldName' =&gt; $myDate));
</code></pre>

<p>Then just proceed with saving the entry as normal.</p>

CC BY-SA 3.0
2017-01-17T11:49:01.040Z
Thank you! I didn't realize I could call `setContent` back to back like that.
Damon
CC BY-SA 3.0
2017-01-17T03:27:01.843Z
2017-01-17T03:27:01.843Z
Jeremy Gimbel
<p>I have a plugin that includes a <a href="https://bootstrap-datepicker.readthedocs.io/en/latest/" rel="nofollow noreferrer">datepicker</a>.</p>

<p>I initialize the datepicker like this</p>

<p><strong>javascript</strong></p>

<pre><code>$('.datepicker').datepicker({
    'format': 'mm/dd/yyyy',
    'todayHighlight': true
});
</code></pre>

<p>I am saving the date from my plugin like this:</p>

<pre><code>$myDate = craft()-&gt;request-&gt;getPost('myDate');
if ($myDate) {
    DateTime::createFromString($myDate, craft()-&gt;timezone);
}

$fieldsLocation = craft()-&gt;request-&gt;getParam('fieldsLocation', 'fields');
$entry-&gt;setContentFromPost($fieldsLocation);
</code></pre>

<p>Because I have designated this field to be required, I get an error back saying that the field cannot be blank. Even though I have chosen a date.</p>

<p>If I change my datepicker to use <em>this</em> format:</p>

<pre><code>$('.datepicker').datepicker({
    'format': 'yyyy/mm/dd',
    'todayHighlight': true
});
</code></pre>

<p>Everything works.
I like how "human/U.S" friendly the top example is vs. <code>yyyy-mm-dd</code>. </p>

<p>Since I am populating the entry with this:</p>

<pre><code>$entry-&gt;setContentFromPost($fieldsLocation);
</code></pre>

<p>Is there a way to convert what is passed in to the correct format Craft is looking for?</p>

<p>I've tried something like:</p>

<pre><code>$date = date(str_replace('/', '-', craft()-&gt;request-&gt;getPost('myDate')));

$myDate = date('Y-m-d', strtotime($date));
</code></pre>

<p>But that is getting overwritten by this guy still:</p>

<pre><code>entry-&gt;setContentFromPost($fieldsLocation);
</code></pre>

<p>Thank you for any suggestions!</p>

<plugin-development><datetime>
How can I convert a date format from my plugin so it saves correctly?
# How can I convert a date format from my plugin so it saves correctly?

(Asked by: username_0 on 2017-01-16)

I have a plugin that includes a datepicker.
I initialize the datepicker like this
javascript
$('.datepicker').datepicker({
    'format': 'mm/dd/yyyy',
    'todayHighlight': true
});

I am saving the date from my plugin like this:
$myDate = craft()->request->getPost('myDate');
if ($myDate) {
    DateTime::createFromString($myDate, craft()->timezone);
}

$fieldsLocation = craft()->request->getParam('fieldsLocation', 'fields');
$entry->setContentFromPost($fieldsLocation);

Because I have designated this field to be required, I get an error back saying that the field cannot be blank. Even though I have chosen a date.
If I change my datepicker to use this format:
$('.datepicker').datepicker({
    'format': 'yyyy/mm/dd',
    'todayHighlight': true
});

Everything works.
I like how "human/U.S" friendly the top example is vs. yyyy-mm-dd. 
Since I am populating the entry with this:
$entry->setContentFromPost($fieldsLocation);

Is there a way to convert what is passed in to the correct format Craft is looking for?
I've tried something like:
$date = date(str_replace('/', '-', craft()->request->getPost('myDate')));

$myDate = date('Y-m-d', strtotime($date));

But that is getting overwritten by this guy still:
entry->setContentFromPost($fieldsLocation);

Thank you for any suggestions!

**Tags:** plugin-development, datetime


---

## 1 answers

### Answer by username_1 on 2017-01-17. Score: 1 | Accepted answer

After $entry->setContentFromPost($fieldsLocation); you should be able to add another line to explicitly set your calculated date.
$entry->setContent(array('fieldName' => $myDate));

Then just proceed with saving the entry as normal.

> **Comments:**
> **username_0**: Thank you! I didn't realize I could call `setContent` back to back like that.  

---

