nkovacs / yii2-datetimepicker

Bootstrap datetimepicker widget for Yii 2
10 stars 7 forks source link

Cannot set format #3

Closed laurabennett closed 8 years ago

laurabennett commented 8 years ago

I want to remove seconds from the time display.

nkovacs commented 8 years ago

You can use format to set a custom format that doesn't include seconds: https://github.com/nkovacs/yii2-datetimepicker/blob/master/DateTimePicker.php#L75

laurabennett commented 8 years ago

Thank you for the quick reply! I tried that. Am I using the wrong syntax?

        <?= $form->field($model,

'arrival_datetime')->widget(DateTimePicker::className(), ['clientOptions' => ['format' => 'YYYY-MM-DD HH:mm', 'sideBySide' => true,]]) ?>

[image: Inline image 2]

On Tue, Sep 6, 2016 at 2:15 PM, Nikola Kovacs notifications@github.com wrote:

You can use format to set a custom format that doesn't include seconds: https://github.com/nkovacs/yii2-datetimepicker/blob/ master/DateTimePicker.php#L75

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/nkovacs/yii2-datetimepicker/issues/3#issuecomment-245040094, or mute the thread https://github.com/notifications/unsubscribe-auth/ATCVJjksCzHAzLm8LF0qeLXnI_0fVjoZks5qna2qgaJpZM4J2Hu2 .

Laura Bennett digital diva

drive > P.O. Box 2838, 170 Kearsarge Street North Conway, NH 03860 603.356.3030 laura@drivebrandstudio.com twitter.com/drivebrands

nkovacs commented 8 years ago

Don't set the format in clientOptions, that will be overridden by the format of the widget. Use the widget's format option:

<?= $form->field($model, 'arrival_datetime')->widget(DateTimePicker::className(),
    'format' => 'y-MM-dd HH:mm'
    ['clientOptions' => ['sideBySide' => true,]]) ?>

The format in this case is ICU: http://userguide.icu-project.org/formatparse/datetime

laurabennett commented 8 years ago

Closed? But how do I get the seconds to go away?? Do I need international locale turned on? Here is a link to my app http://dev1.skyball.drivedev.net/backend/web/attendee-flight/create?attendee_event_id=17

On Tue, Sep 6, 2016 at 2:15 PM, Nikola Kovacs notifications@github.com wrote:

Closed #3 https://github.com/nkovacs/yii2-datetimepicker/issues/3.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/nkovacs/yii2-datetimepicker/issues/3#event-779762360, or mute the thread https://github.com/notifications/unsubscribe-auth/ATCVJjksCzHAzLm8LF0qeLXnI_0fVjoZks5qna2qgaJpZM4J2Hu2 .

Laura Bennett digital diva

drive > P.O. Box 2838, 170 Kearsarge Street North Conway, NH 03860 603.356.3030 laura@drivebrandstudio.com twitter.com/drivebrands

laurabennett commented 8 years ago

Perfect Thanks!!

On Tue, Sep 6, 2016 at 2:23 PM, Nikola Kovacs notifications@github.com wrote:

Don't set the format in clientOptions, that will be overridden by the format of the widget. Use the widget's format option:

<?= $form->field($model, 'arrival_datetime')->widget(DateTimePicker::className(), 'format' => 'y-MM-dd HH:mm' ['clientOptions' => ['sideBySide' => true,]]) ?>

The format in this case is ICU: http://userguide.icu-project. org/formatparse/datetime

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/nkovacs/yii2-datetimepicker/issues/3#issuecomment-245042600, or mute the thread https://github.com/notifications/unsubscribe-auth/ATCVJiqOzAYraiTQ85W0Rm4RAJgX-Rgjks5qna-NgaJpZM4J2Hu2 .

Laura Bennett digital diva

drive > P.O. Box 2838, 170 Kearsarge Street North Conway, NH 03860 603.356.3030 laura@drivebrandstudio.com twitter.com/drivebrands

laurabennett commented 8 years ago

But now the output format is messed up. Is there another option I need to set for that?

[image: Inline image 1]

On Tue, Sep 6, 2016 at 2:40 PM, Laura Bennett laura@drivebrandstudio.com wrote:

Perfect Thanks!!

On Tue, Sep 6, 2016 at 2:23 PM, Nikola Kovacs notifications@github.com wrote:

Don't set the format in clientOptions, that will be overridden by the format of the widget. Use the widget's format option:

<?= $form->field($model, 'arrival_datetime')->widget(DateTimePicker::className(), 'format' => 'y-MM-dd HH:mm' ['clientOptions' => ['sideBySide' => true,]]) ?>

The format in this case is ICU: http://userguide.icu-project.o rg/formatparse/datetime

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/nkovacs/yii2-datetimepicker/issues/3#issuecomment-245042600, or mute the thread https://github.com/notifications/unsubscribe-auth/ATCVJiqOzAYraiTQ85W0Rm4RAJgX-Rgjks5qna-NgaJpZM4J2Hu2 .

Laura Bennett digital diva

drive > P.O. Box 2838, 170 Kearsarge Street North Conway, NH 03860 603.356.3030 laura@drivebrandstudio.com twitter.com/drivebrands

Laura Bennett digital diva

drive > P.O. Box 2838, 170 Kearsarge Street North Conway, NH 03860 603.356.3030 laura@drivebrandstudio.com twitter.com/drivebrands

laurabennett commented 8 years ago

Nevermind, found it. Thanks again.

On Tue, Sep 6, 2016 at 2:48 PM, Laura Bennett laura@drivebrandstudio.com wrote:

But now the output format is messed up. Is there another option I need to set for that?

[image: Inline image 1]

On Tue, Sep 6, 2016 at 2:40 PM, Laura Bennett laura@drivebrandstudio.com wrote:

Perfect Thanks!!

On Tue, Sep 6, 2016 at 2:23 PM, Nikola Kovacs notifications@github.com wrote:

Don't set the format in clientOptions, that will be overridden by the format of the widget. Use the widget's format option:

<?= $form->field($model, 'arrival_datetime')->widget(DateTimePicker::className(), 'format' => 'y-MM-dd HH:mm' ['clientOptions' => ['sideBySide' => true,]]) ?>

The format in this case is ICU: http://userguide.icu-project.o rg/formatparse/datetime

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/nkovacs/yii2-datetimepicker/issues/3#issuecomment-245042600, or mute the thread https://github.com/notifications/unsubscribe-auth/ATCVJiqOzAYraiTQ85W0Rm4RAJgX-Rgjks5qna-NgaJpZM4J2Hu2 .

Laura Bennett digital diva

drive > P.O. Box 2838, 170 Kearsarge Street North Conway, NH 03860 603.356.3030 laura@drivebrandstudio.com twitter.com/drivebrands

Laura Bennett digital diva

drive > P.O. Box 2838, 170 Kearsarge Street North Conway, NH 03860 603.356.3030 laura@drivebrandstudio.com twitter.com/drivebrands

Laura Bennett digital diva

drive > P.O. Box 2838, 170 Kearsarge Street North Conway, NH 03860 603.356.3030 laura@drivebrandstudio.com twitter.com/drivebrands

laurabennett commented 8 years ago

Sorry to keep bothering you, do you have a recommendation for how to save this to database? When I save and reopen the time portion isn't correct. Do I need to use a formatter? Thanks Again, Laura

On Tue, Sep 6, 2016 at 2:54 PM, Laura Bennett laura@drivebrandstudio.com wrote:

Nevermind, found it. Thanks again.

On Tue, Sep 6, 2016 at 2:48 PM, Laura Bennett laura@drivebrandstudio.com wrote:

But now the output format is messed up. Is there another option I need to set for that?

[image: Inline image 1]

On Tue, Sep 6, 2016 at 2:40 PM, Laura Bennett <laura@drivebrandstudio.com

wrote:

Perfect Thanks!!

On Tue, Sep 6, 2016 at 2:23 PM, Nikola Kovacs notifications@github.com wrote:

Don't set the format in clientOptions, that will be overridden by the format of the widget. Use the widget's format option:

<?= $form->field($model, 'arrival_datetime')->widget(DateTimePicker::className(), 'format' => 'y-MM-dd HH:mm' ['clientOptions' => ['sideBySide' => true,]]) ?>

The format in this case is ICU: http://userguide.icu-project.o rg/formatparse/datetime

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/nkovacs/yii2-datetimepicker/issues/3#issuecomment-245042600, or mute the thread https://github.com/notifications/unsubscribe-auth/ATCVJiqOzAYraiTQ85W0Rm4RAJgX-Rgjks5qna-NgaJpZM4J2Hu2 .

Laura Bennett digital diva

drive > P.O. Box 2838, 170 Kearsarge Street North Conway, NH 03860 603.356.3030 laura@drivebrandstudio.com twitter.com/drivebrands

Laura Bennett digital diva

drive > P.O. Box 2838, 170 Kearsarge Street North Conway, NH 03860 603.356.3030 laura@drivebrandstudio.com twitter.com/drivebrands

Laura Bennett digital diva

drive > P.O. Box 2838, 170 Kearsarge Street North Conway, NH 03860 603.356.3030 laura@drivebrandstudio.com twitter.com/drivebrands

Laura Bennett digital diva

drive > P.O. Box 2838, 170 Kearsarge Street North Conway, NH 03860 603.356.3030 laura@drivebrandstudio.com twitter.com/drivebrands

nkovacs commented 8 years ago

You need to use the validator's timestampAttribute property to get a parsed value (defaults to a unix timestamp, but you can change it, check the Yii 2 documentation for the DateValidator). Then save that value to the database.

The way I do it is I have a database field (e.g. created_at) and a property on the model class (e.g. created_at_input). Before rendering the widget (in the controller's action method) I format the field's value using the same format that the widget uses (using Yii::$app->formatter->asDateTime), and put that into the property (e.g. $model->created_at_input = Yii::$app->formatter->asDatetime($model->created_at, $format), but you also need to check for null). Then the validator is set on the property (created_at_input) and timestampAttribute is set to the field (created_at). If the input is valid, the new value will be converted into a format that the db can understand and saved into the db field. Then I can just call $model->save.

laurabennett commented 8 years ago

I am using the formatter in the model for regular date widget fields but when I tried editing it to work for datetime it didn't work. Do I have to use the validator? Am I just using the wrong format here?

    $this->departure_datetime = isset($this->departure_datetime) ?

Yii::$app->formatter->asDatetime($this->departure_datetime, 'php: Y-m-d h:i a') : '';

On Tue, Sep 6, 2016 at 3:28 PM, Nikola Kovacs notifications@github.com wrote:

You need to use the validator's timestampAttribute property to get a parsed value (defaults to a unix timestamp, but you can change it, check the Yii 2 documentation for the DateValidator). Then save that value to the database.

The way I do it is I have a database field (e.g. created_at) and a property on the model class (e.g. created_at_display). Before rendering the widget (in the controller's action method) I format the field's value using the same format that the widget uses (using Yii::$app->formatter-> asDateTime), and put that into the property (e.g. $model->created_at_display = Yii::$app->formatter->asDatetime($model->created_at, $format)). Then the validator is set on the property (created_at_display) and timestampAttribute is set to the field (created_at). If the input is valid, the new value will be converted into a format that the db can understand and saved into the db field. Then I can just call $model->save.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/nkovacs/yii2-datetimepicker/issues/3#issuecomment-245062191, or mute the thread https://github.com/notifications/unsubscribe-auth/ATCVJi5lfW7Essn1matxz3Tr4jC_ODN6ks5qnb7SgaJpZM4J2Hu2 .

Laura Bennett digital diva

drive > P.O. Box 2838, 170 Kearsarge Street North Conway, NH 03860 603.356.3030 laura@drivebrandstudio.com twitter.com/drivebrands

nkovacs commented 8 years ago

What do you mean it does not work? You have to use the exact same format in asDatetime as in the widget's format option.

laurabennett commented 8 years ago

Specifically the hour isn't saving correctly. I'm trying to put in 4:30 am but the output is 2016-09-13 00:30 and then it shows up in the widget as 8:30pm. I tried changing the format so this is what my code is currently.

    $this->arrival_datetime = isset($this->arrival_datetime) ?

Yii::$app->formatter->asDatetime($this->arrival_datetime, 'php:Y-m-d H:i') : '';

        <?= $form->field($model,

'arrival_datetime')->widget(DateTimePicker::className(), ['format' => 'php:Y-m-d H:i', 'clientOptions' => ['sideBySide' => true,]]) ?>

On Tue, Sep 6, 2016 at 3:52 PM, Nikola Kovacs notifications@github.com wrote:

What do you mean it does not work? You have to use the exact same format in asDatetime as in the widget's format option.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/nkovacs/yii2-datetimepicker/issues/3#issuecomment-245069094, or mute the thread https://github.com/notifications/unsubscribe-auth/ATCVJuji_rSVPMmbPPjM-hEqLirAhNMjks5qncRlgaJpZM4J2Hu2 .

Laura Bennett digital diva

drive > P.O. Box 2838, 170 Kearsarge Street North Conway, NH 03860 603.356.3030 laura@drivebrandstudio.com twitter.com/drivebrands

nkovacs commented 8 years ago

You shouldn't use the same property, because when you format it like that, your database probably won't understand it.

What's your datevalidator configuration, and what's the field type in the database?

laurabennett commented 8 years ago

MYSQL datetime YYYY-MM-DD HH:mm:SS

It's saving everything except the hour properly. Where do I find the datevalidator configuration? I haven't set any rules or validators on the field, just trying to set the format in the beforeSave method. You said to use the exact same format, so now I'm confused...

On Tue, Sep 6, 2016 at 4:04 PM, Nikola Kovacs notifications@github.com wrote:

You shouldn't use the same property, because when you format it like that, your database probably won't understand it.

What's your datevalidator configuration, and what's the field type in the database?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/nkovacs/yii2-datetimepicker/issues/3#issuecomment-245073025, or mute the thread https://github.com/notifications/unsubscribe-auth/ATCVJlzG9LKofVDKtyZ85UhHd94MnKrmks5qncdhgaJpZM4J2Hu2 .

Laura Bennett digital diva

drive > P.O. Box 2838, 170 Kearsarge Street North Conway, NH 03860 603.356.3030 laura@drivebrandstudio.com twitter.com/drivebrands

nkovacs commented 8 years ago

Your model should have an extra property (the database table has a field called arrival_datetime):

class Foo extends ActiveRecord {
    public $arrival_datetime_input;
}

In your controller (this only works if asDatetime can parse the mysql format, which I think it can):

$model->arrival_datetime_input = isset($this->arrival_datetime) ?
Yii::$app->formatter->asDatetime($this->arrival_datetime, 'php:Y-m-d H:i')
: '';

In your view:

<?= $form->field($model,
'arrival_datetime_input')->widget(DateTimePicker::className(),
                ['format' => 'php:Y-m-d H:i', 'clientOptions' =>
['sideBySide' => true,]]) ?>

And in your validator, set up timestampAttribute and timestampAttributeFormat: http://www.yiiframework.com/doc-2.0/yii-validators-datevalidator.html#$timestampAttribute-detail Set timestampAttribute to 'arrival_datetime'.

It needs to convert it to YYYY-MM-DD HH:mm:SS so mysql can understand it.

laurabennett commented 8 years ago

It seems to be just the hours that aren't displaying properly. They are saving to the database. So I must have a problem with timezone. Thanks again for your help. -Laura

On Tue, Sep 6, 2016 at 5:14 PM, Nikola Kovacs notifications@github.com wrote:

Your model should have an extra property (the database table has a field called arrival_datetime):

class Foo extends ActiveRecord { public $arrival_datetime_input; }

In your controller (this only works if asDatetime can parse the mysql format, which I think it can):

$model->arrival_datetime_input = isset($this->arrival_datetime) ? Yii::$app->formatter->asDatetime($this->arrival_datetime, 'php:Y-m-d H:i') : '';

In your view:

<?= $form->field($model, 'arrival_datetime')->widget(DateTimePicker::className(), ['format' => 'php:Y-m-d H:i', 'clientOptions' => ['sideBySide' => true,]]) ?>

And in your validator, set up timestampAttribute and timestampAttributeFormat: http://www.yiiframework.com/doc-2.0/yii-validators-datevalidator.html#$ timestampAttribute-detail Set timestampAttribute to 'arrival_datetime'.

It needs to convert it to YYYY-MM-DD HH:mm:SS so mysql can understand it.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/nkovacs/yii2-datetimepicker/issues/3#issuecomment-245093763, or mute the thread https://github.com/notifications/unsubscribe-auth/ATCVJop0J9hHLbvyJga6489_0TKRAlCNks5qndedgaJpZM4J2Hu2 .

Laura Bennett digital diva

drive > P.O. Box 2838, 170 Kearsarge Street North Conway, NH 03860 603.356.3030 laura@drivebrandstudio.com twitter.com/drivebrands

laurabennett commented 8 years ago

Nikola, I'm not able to save a blank date. It gets saved as 1969-12-31 19:00:00. Is that something the validator will fix? I'm still not clear on how to use the validator? Thanks, Laura

On Wed, Sep 7, 2016 at 9:29 AM, Laura Bennett laura@drivebrandstudio.com wrote:

It seems to be just the hours that aren't displaying properly. They are saving to the database. So I must have a problem with timezone. Thanks again for your help. -Laura

On Tue, Sep 6, 2016 at 5:14 PM, Nikola Kovacs notifications@github.com wrote:

Your model should have an extra property (the database table has a field called arrival_datetime):

class Foo extends ActiveRecord { public $arrival_datetime_input; }

In your controller (this only works if asDatetime can parse the mysql format, which I think it can):

$model->arrival_datetime_input = isset($this->arrival_datetime) ? Yii::$app->formatter->asDatetime($this->arrival_datetime, 'php:Y-m-d H:i') : '';

In your view:

<?= $form->field($model, 'arrival_datetime')->widget(DateTimePicker::className(), ['format' => 'php:Y-m-d H:i', 'clientOptions' => ['sideBySide' => true,]]) ?>

And in your validator, set up timestampAttribute and timestampAttributeFormat: http://www.yiiframework.com/doc-2.0/yii-validators-datevalid ator.html#$timestampAttribute-detail Set timestampAttribute to 'arrival_datetime'.

It needs to convert it to YYYY-MM-DD HH:mm:SS so mysql can understand it.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/nkovacs/yii2-datetimepicker/issues/3#issuecomment-245093763, or mute the thread https://github.com/notifications/unsubscribe-auth/ATCVJop0J9hHLbvyJga6489_0TKRAlCNks5qndedgaJpZM4J2Hu2 .

Laura Bennett digital diva

drive > P.O. Box 2838, 170 Kearsarge Street North Conway, NH 03860 603.356.3030 laura@drivebrandstudio.com twitter.com/drivebrands

Laura Bennett digital diva

drive > P.O. Box 2838, 170 Kearsarge Street North Conway, NH 03860 603.356.3030 laura@drivebrandstudio.com twitter.com/drivebrands

nkovacs commented 8 years ago

Add this to your model's rules:

['arrival_datetime_input', 'datetime', 'format' => 'php:Y-m-d H:i', 'timestampAttribute' => 'arrival_datetime', 'timestampAttributeFormat' => 'y-MM-dd HH:mm:ss'],
laurabennett commented 8 years ago

It throws an error.

Exception (Invalid Configuration) 'yii\base\InvalidConfigException' with message 'Missing required parameter "time" when instantiating "DateTime".'

On Wed, Sep 7, 2016 at 3:06 PM, Nikola Kovacs notifications@github.com wrote:

Add this to your model's rules:

['arrival_datetime_input', 'datetime', 'format' => 'php:Y-m-d H:i', 'timestampAttribute' => 'arrival_datetime', 'timestampAttributeFormat' => 'y-MM-dd HH:mm:ss'],

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/nkovacs/yii2-datetimepicker/issues/3#issuecomment-245383926, or mute the thread https://github.com/notifications/unsubscribe-auth/ATCVJkBJSXasEalQ6hKHqfNIf0n6S8OAks5qnwstgaJpZM4J2Hu2 .

Laura Bennett digital diva

drive > P.O. Box 2838, 170 Kearsarge Street North Conway, NH 03860 603.356.3030 laura@drivebrandstudio.com twitter.com/drivebrands

nkovacs commented 8 years ago

Have you set up the validators correctly?

laurabennett commented 8 years ago

I don't understand how to setup the validators. The documentation says to add a line in app config but I don't know what file that is, config/main.php?

On Wed, Sep 7, 2016 at 3:35 PM, Nikola Kovacs notifications@github.com wrote:

Have you set up the validators correctly https://github.com/nkovacs/yii2-datetimepicker#validators?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/nkovacs/yii2-datetimepicker/issues/3#issuecomment-245391972, or mute the thread https://github.com/notifications/unsubscribe-auth/ATCVJjiS9dfxFtm5wPbcDjiih_ryOkcdks5qnxH6gaJpZM4J2Hu2 .

Laura Bennett digital diva

drive > P.O. Box 2838, 170 Kearsarge Street North Conway, NH 03860 603.356.3030 laura@drivebrandstudio.com twitter.com/drivebrands

nkovacs commented 8 years ago

Yes.

laurabennett commented 8 years ago

No change. Its still saving a 1969 date instead of all zeros.

On Wed, Sep 7, 2016 at 3:47 PM, Nikola Kovacs notifications@github.com wrote:

Yes.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/nkovacs/yii2-datetimepicker/issues/3#issuecomment-245395274, or mute the thread https://github.com/notifications/unsubscribe-auth/ATCVJsEuWlknNNejat5q83JGlO7M0YEtks5qnxTtgaJpZM4J2Hu2 .

Laura Bennett digital diva

drive > P.O. Box 2838, 170 Kearsarge Street North Conway, NH 03860 603.356.3030 laura@drivebrandstudio.com twitter.com/drivebrands