<p>This is how it works for me (please don't ask why!) ...</p>

<pre><code>protected function defineSettings()
{
    return array(
        'myOpenings' =&gt; array(AttributeType::Number, 'required' =&gt; true),
        'myStartDate' =&gt; AttributeType::Mixed,
        'myEndDate' =&gt; AttributeType::Mixed,
    );
}

public function getSettingsHtml()
{
    $settings = $this-&gt;getSettings();

    $settings['myStartDate'] = DateTime::createFromString($settings['myStartDate']);
    $settings['myEndDate'] = DateTime::createFromString($settings['myEndDate']);

    return craft()-&gt;templates-&gt;render('plugin/_settings', array(
        'settings' =&gt; $settings
    ));
}
</code></pre>

CC BY-SA 3.0
2015-04-09T16:33:32.803Z
No worries at all - It was probably something I am/was doing. Thank you so much for your help!
Damon
CC BY-SA 3.0
2015-04-09T07:27:52.773Z
2015-04-09T07:27:52.773Z
carlcs
<p><strong>Update:</strong> Sorry dragonslovetacos, I just tested this and found out it doesn't work as expected. It probably is the right way to handle DateTime values <em>when</em> saved with attribute type DateTime.</p>

<hr>

<p>You're probably not preparing the value for your template. Before saving your date to the database you convert it to a DateTime object and your system timezone gets calculated into the value. You now need to do the same thing before you render that value in your template.</p>

<p>Or maybe it's the other way round, and you're preparing the value for your template and save it unprepared.</p>

<p>Anyways, the helper function <code>DateTime::createFromString()</code> should be applied to your value in both, <code>prepSettings()</code> and <code>getSettingsHtml()</code>.</p>

CC BY-SA 3.0
2015-04-08T18:41:51.163Z
2015-04-09T07:20:07.027Z
2015-04-09T07:20:07.027Z
carlcs
<p>I've used <a href="https://craftcms.stackexchange.com/questions/8348/how-do-i-save-a-plugins-date-settings-field/8358#8358">this thread</a> to successfully get my dateTime input fields to repopulate.</p>

<p>The problem I'm seeing now is that while values in the DB are correct, the field input values are always a day behind. </p>

<p>For example, the data in my DB looks like:</p>

<pre><code>....
"startDate":{"date":"5\/4\/2015"}
....
</code></pre>

<p>But, the value that is getting rendered in the filed (from my settings page) is this:</p>

<pre><code>5/3/2015
</code></pre>

<p>If I save the settings (no errors). The values in the DB will be <code>5/3/2015</code> - but then the field on my settings page will show: <code>5/2/2015</code></p>

<p><strong>EDIT</strong></p>

<p>Thank you @carlcs, I am sure you are right - I'm just not doing it...right.</p>

<p>Here is what I have tried:</p>

<p><strong>MyPlugin.php</strong></p>

<pre><code>// this returns nothing when logged in error_log
$settings['myStartDate'] = DateTime::createFromString($settings['seasonStartDate']['date'] );

// this returns the date, but one day in the past
$settings['myStartDate'] = new DateTime( $settings['myStartDate']['date'] );
</code></pre>

<p>In my <code>defineSettings()</code> method, I am setting <code>myStartDate</code> to a <code>Mixed</code> type as there is a bug with <code>dateTime</code>.</p>

<pre><code>  ....
  'myStartDate' =&gt; array(
       AttributeType::Mixed
   ),
   ....
</code></pre>

<p>I'm not exactly sure where <code>prepSettings()</code> should live. Here is the whole file:</p>

<p><strong>MyPlugin.php</strong></p>

<pre><code>public function defineSettings()
    {
        return array(
            'myOpenings' =&gt; array(
                AttributeType::Number,
                'required' =&gt; true
            ),
            'myStartDate' =&gt; array(
                AttributeType::Mixed
            ),
            'myEndDate' =&gt; array(
                AttributeType::Mixed
            )
        );
    }

    public function getSettingsHtml()
    {
        $settings = $this-&gt;getSettings();


        if ( isset( $settings['myStartDate']['date'] ) )
        {
            $settings['myStartDate'] = DateTime::createFromString( $settings['myStartDate']['date'] );
         // $settings['myStartDate'] = new DateTime( $settings['myStartDate']['date'] );

            error_log($settings['myStartDate']);
        }

        if ( isset( $settings['myEndDate']['date'] ) )
        {
            $settings['myEndDate'] = new DateTime( $settings['myEndDate']['date'] );
        }

        return craft()-&gt;templates-&gt;render( 'plugin/_settings', array(
                    'settings' =&gt; $settings,
                ) );
    }
</code></pre>

<p><strong>EDIT/SOLUTION</strong></p>

<p>@carlcs and @Mats Mikkel Rummelhoff for their help. 
For anyone coming to this post, here is what my "final" main plugin class file looks like hope it helps.</p>

<p><strong>MyPlugin.php</strong></p>

<pre><code>....
public function defineSettings() {
    return array(
        'myOpenings' =&gt; array(AttributeType::Number, 'required'=&gt;true),
        'myStartDate' =&gt; array(AttributeType::Mixed, 'required'=&gt;true),
        'myEndDate' =&gt; array(AttributeType::Mixed, 'required'=&gt;true)
    );
}

public function getSettingsHtml() {
    $settings = $this-&gt;getSettings();

    if(isset($settings['myStartDate']['date']))
    {
        $settings['myStartDate'] = DateTime::CreateFromString($settings['myStartDate']);
    }

    if(isset($settings['myEndDate']['date']))
    {
        $settings['myEndDate'] = DateTime::CreateFromString($settings['myEndDate']);
    }

    return craft()-&gt;templates-&gt;render('myplugin/_settings', array(
        'settings' =&gt; $settings
    ));
} 
</code></pre>

CC BY-SA 3.0
2015-04-09T01:22:03.013Z
```prepSettings()``` goes in your main plugin class, see http://buildwithcraft.com/docs/plugins/plugin-settings
Mats Mikkel Rummelhoff
<plugin-development><datetime>
dateTime values in settings page are always a day behind
# dateTime values in settings page are always a day behind

(Asked by: username_0 on 2015-04-08)

I've used this thread to successfully get my dateTime input fields to repopulate.
The problem I'm seeing now is that while values in the DB are correct, the field input values are always a day behind. 
For example, the data in my DB looks like:
....
"startDate":{"date":"5\/4\/2015"}
....

But, the value that is getting rendered in the filed (from my settings page) is this:
5/3/2015

If I save the settings (no errors). The values in the DB will be 5/3/2015 - but then the field on my settings page will show: 5/2/2015
EDIT
Thank you @carlcs, I am sure you are right - I'm just not doing it...right.
Here is what I have tried:
MyPlugin.php
// this returns nothing when logged in error_log
$settings['myStartDate'] = DateTime::createFromString($settings['seasonStartDate']['date'] );

// this returns the date, but one day in the past
$settings['myStartDate'] = new DateTime( $settings['myStartDate']['date'] );

In my defineSettings() method, I am setting myStartDate to a Mixed type as there is a bug with dateTime.
  ....
  'myStartDate' => array(
       AttributeType::Mixed
   ),
   ....

I'm not exactly sure where prepSettings() should live. Here is the whole file:
MyPlugin.php
public function defineSettings()
    {
        return array(
            'myOpenings' => array(
                AttributeType::Number,
                'required' => true
            ),
            'myStartDate' => array(
                AttributeType::Mixed
            ),
            'myEndDate' => array(
                AttributeType::Mixed
            )
        );
    }

    public function getSettingsHtml()
    {
        $settings = $this->getSettings();


        if ( isset( $settings['myStartDate']['date'] ) )
        {
            $settings['myStartDate'] = DateTime::createFromString( $settings['myStartDate']['date'] );
         // $settings['myStartDate'] = new DateTime( $settings['myStartDate']['date'] );

            error_log($settings['myStartDate']);
        }

        if ( isset( $settings['myEndDate']['date'] ) )
        {
            $settings['myEndDate'] = new DateTime( $settings['myEndDate']['date'] );
        }

        return craft()->templates->render( 'plugin/_settings', array(
                    'settings' => $settings,
                ) );
    }

EDIT/SOLUTION
@carlcs and @Mats Mikkel Rummelhoff for their help. 
For anyone coming to this post, here is what my "final" main plugin class file looks like hope it helps.
MyPlugin.php
....
public function defineSettings() {
    return array(
        'myOpenings' => array(AttributeType::Number, 'required'=>true),
        'myStartDate' => array(AttributeType::Mixed, 'required'=>true),
        'myEndDate' => array(AttributeType::Mixed, 'required'=>true)
    );
}

public function getSettingsHtml() {
    $settings = $this->getSettings();

    if(isset($settings['myStartDate']['date']))
    {
        $settings['myStartDate'] = DateTime::CreateFromString($settings['myStartDate']);
    }

    if(isset($settings['myEndDate']['date']))
    {
        $settings['myEndDate'] = DateTime::CreateFromString($settings['myEndDate']);
    }

    return craft()->templates->render('myplugin/_settings', array(
        'settings' => $settings
    ));
}

**Tags:** plugin-development, datetime
> **Comments:**
> **username_1**: ```prepSettings()``` goes in your main plugin class, see http://buildwithcraft.com/docs/plugins/plugin-settings  

---

## 2 answers

### Answer by username_2 on 2015-04-09. Score: 2 | Accepted answer

This is how it works for me (please don't ask why!) ...
protected function defineSettings()
{
    return array(
        'myOpenings' => array(AttributeType::Number, 'required' => true),
        'myStartDate' => AttributeType::Mixed,
        'myEndDate' => AttributeType::Mixed,
    );
}

public function getSettingsHtml()
{
    $settings = $this->getSettings();

    $settings['myStartDate'] = DateTime::createFromString($settings['myStartDate']);
    $settings['myEndDate'] = DateTime::createFromString($settings['myEndDate']);

    return craft()->templates->render('plugin/_settings', array(
        'settings' => $settings
    ));
}

> **Comments:**
> **username_0**: No worries at all - It was probably something I am/was doing. Thank you so much for your help!  

---

### Answer by username_2 on 2015-04-08. Score: 3

**Update:** Sorry dragonslovetacos, I just tested this and found out it doesn't work as expected. It probably is the right way to handle DateTime values *when* saved with attribute type DateTime.

---

You're probably not preparing the value for your template. Before saving your date to the database you convert it to a DateTime object and your system timezone gets calculated into the value. You now need to do the same thing before you render that value in your template.

Or maybe it's the other way round, and you're preparing the value for your template and save it unprepared.

Anyways, the helper function `DateTime::createFromString()` should be applied to your value in both, `prepSettings()` and `getSettingsHtml()`.


---

