1

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
    ));
} 
2

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
    ));
}
  • No worries at all - It was probably something I am/was doing. Thank you so much for your help! – Damon Apr 9 '15 at 16:33
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().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.