Sanjay Chaudhari
Sanjay Chaudhari

Reputation: 420

Yii2: kartik/DateTimePicker prevent selection of a date before today

I have tried with below code and it is not working for me.

echo $form->field($model, 'value')->widget(DateTimePicker::classname(), [
      'pluginOptions' => [
           'autoclose'      => true,
           'format'         => 'yyyy-mm-dd HH:ii:ss',
           'minuteStep'     => 1,
           'todayHighlight' => true,
           'startDate'      => date("y-m-d H:i:s"),
           'changeYear'     => true,
           'changeMonth'    => true,
      ]
])->label('Select date & time');

Upvotes: 1

Views: 3728

Answers (4)

Muhammad Omer Aslam
Muhammad Omer Aslam

Reputation: 23738

You can use the following i assume you are using kartik\widgets\DateTimePicker

$form->field($model, 'campaign_schedule', [
    'inputOptions' =>
    [
        'value' => date('Y-m-d H:i:s'),
    ],
])->widget(kartik\widgets\DateTimePicker::class, [
    'options' => ['placeholder' => 'Select operating time ...'],
    'convertFormat' => false,
    'pluginOptions' => [
        'format' => 'yyyy-mm-dd hh:ii:ss',
        'todayHighlight' => true,
        'startDate' => new JsExpression("new Date('" . date('m/d/y') . "')"),
        'autoclose' => true,
    ],
])

Upvotes: 0

Patrick R
Patrick R

Reputation: 6857

It works for me as below:

$form->field($model, 'value')->widget(DateTimePicker::classname(), [
    'options' => ['placeholder' => 'Enter Value'],
    'pluginOptions' => [
        'autoclose' => true,
        'format' => 'Y-m-d H:i:s',
        'startDate' => date('Y-m-d H:i:s')
    ]
]);

Upvotes: 2

Balu Abraham
Balu Abraham

Reputation: 40

  echo $form->field($model, 'value')->widget(DateTimePicker::classname(), [
          'pluginOptions' => [
               'minuteStep'     => 1,
               'minDate'        => 0,
               'startDate'      => date("y-m-d H:i:s"),
          ]
    ])

Upvotes: 0

Nana Partykar
Nana Partykar

Reputation: 10548

Add 'minDate' => 0 to disable previous date selection from today.

Updated Code

echo $form->field($model, 'value')->widget(DateTimePicker::classname(), [
      'pluginOptions' => [
           'autoclose'      => true,
           'format'         => 'yyyy-mm-dd HH:ii:ss',
           'minuteStep'     => 1,
           'minDate'        => 0,
           'todayHighlight' => true,
           'startDate'      => date("y-m-d H:i:s"),
           'changeYear'     => true,
           'changeMonth'    => true,
      ]
])->label('Select date & time');

Upvotes: 0

Related Questions