I have start_date and end_date columns in my model.
In the form I put:
<?= $form->field($model, 'start_date')->widget(
DatePicker::className(), [
'inline' => false,
'template' => '<div class="well well-sm" style="background-color: #fff; width:250px">{input}</div>',
'clientOptions' => [
'autoclose' => true,
'format' => 'dd-M-yyyy'
]
]);?>
It's saving null parameters in start_date and end_date columns.
P.S. Bth, how to hide a calendar? I want to make it hidden, and when I click on input it will be automatically shown to choose date.
Try this
<?= $form->field($model, 'start_date')->widget(
DatePicker::className(), [
'inline' => false,
// remove the template
'clientOptions' => [
'autoclose' => true,
'format' => 'yyyy-mm-dd' //change the date format
]
]);?>
The calender remained open because of the template. Use yyyy-mm-dd date format to save the date
Related
I use kartik DatePicker in my activeform.
use kartik\date\DatePicker;
My activeform field:
<?= $form->field($model, 'transferred_date')->widget(DatePicker::className(), [
'value' => date('d-M-Y', strtotime('+2 days')),
'options' => ['placeholder' => 'Select date ...'],
'pluginOptions' => [
'format' => 'dd-mm-yyyy',
'todayHighlight' => true
]
])->label('Transferred Date');
?>
When I hover the mouse on calendar icon, it shows a tool tip like this.
I have to remove the tooltip. How can I?
When reading the doc you can read this in the settings:
pickerButton: mixed the calendar picker button configuration - applicable only when type is set to DatePicker::TYPE_COMPONENT_PREPEND or DatePicker::TYPE_COMPONENT_APPEND. This can be one of the following types:
string, if this is a string, it will be displayed as is (and will not be HTML encoded).
boolean, if this is set to false, it will not be displayed.
array, this is the default behavior. If passed as an array, it will be considered as HTML attributes for the picker button addon. The following special keys are recognized:
icon, string the bootstrap glyphicon name/suffix. Defaults to 'calendar'.
title, string|boolean the title to be displayed on hover. Defaults to 'Select date & time'. If this is set to false, it will not be displayed.
So i can say without testing that it should be something like this:
<?= $form->field($model, 'transferred_date')
->widget(DatePicker::className(), [
'type' => DatePicker::TYPE_COMPONENT_PREPEND,
'pickerButton' => ['title' => false],
'value' => date('d-M-Y', strtotime('+2 days')),
'options' => ['placeholder' => 'Select date ...'],
'pluginOptions' => [
'format' => 'dd-mm-yyyy',
'todayHighlight' => true
]
])->label('Transferred Date');
?>
So you missed this in the config:
'type' => DatePicker::TYPE_COMPONENT_PREPEND,
'pickerButton' => ['title' => false],
I'm able to highlight current date with 'todayHighlight' => true, but I want to highlight some other date.
You can use this function beforeShowDay as pluginOptions array key.
Here is simple example:
<?= $form->field($model, 'myDate', ['showLabels' => false])->widget(DatePicker::className(), [
'size' => 'sm',
'type' => DatePicker::TYPE_INPUT,
'pluginOptions' => [
'autoclose' => true,
'format' => 'Y-m-d',
'startDate' => date('Y-m-d'),
'endDate' => date('Y-m-d', strtotime('-1 day')),
'weekStart' => 1,
'beforeShowDay' => new \yii\web\JsExpression("function (dates) {
console.log(dates); // List with available dates as date object defined with startDate/endDate (for mor customisations if needed)
return {classes: 'highlight', tooltip: 'Pick this day'};
}
}"),
]
]); ?>
Don't forget to define class (.highlight in this case) in your css.
.highlight{background: #ebf4f8}
I use datepicker to create an input in a form.
I use like this :
<?=
$form->field($model, 'DATE_BIRTH')->widget(
DatePicker::className(), [
// inline too, not bad
'inline' => true,
// modify template for custom rendering
'template' => '<div class="well well-sm" style="background-color: #fff; width:250px">{input}</div>',
'clientOptions' => [
'autoclose' => true,
'format' => 'dd-mm-yyyy',
'todayBtn' => true
]
]);
?>
But when insert into database, it gives me error coz incorrect value.
Mysql have a format "yyyy-mm-dd".
How can I still get my date format in datepicker, coz in Indonesia, we know 'dd-mm-yyyy' .
Thanks
You can use beforeSave method in your model file to change the date format before saving into the database like below.
public function beforeSave($insert) {
//if you want only on insert you can use this if condition else remove the if condition.
if($insert){
$this->DATE_BIRTH = Yii::$app->formatter->asDatetime(strtotime($this->DATE_BIRTH), "php:Y-m-d");
}
return parent::beforeSave($insert);
}
kartik date control is not setting the date. If a date of tuesday and thursday is selected; If I check the value of the hidden input field they are set to false when one of these two days is selected. Please!!!! I need your help I am stuck.
The version is dev-master that I dowloaded one month ago.
Here is how I configured the Extension:
'datecontrol' => [
'class' => 'kartik\datecontrol\Module',
// format settings for displaying each date attribute (ICU format example)
'displaySettings' => [
Module::FORMAT_DATE => 'php:D d-M-Y',
Module::FORMAT_TIME => 'php:H:i:s',
Module::FORMAT_DATETIME => 'php:D d-M-Y H:i:s',
],
// format settings for saving each date attribute (PHP format example)
'saveSettings' => [
Module::FORMAT_DATE => 'php:Y-m-d',
Module::FORMAT_TIME => 'php:H:i:s',
Module::FORMAT_DATETIME => 'php:Y-m-d H:i:s',
],
// set your display timezone
// 'displayTimezone' => 'Asia/Kolkata',
// set your timezone for date saved to db
// 'saveTimezone' => 'UTC',
// automatically use kartik\widgets for each of the above formats
'autoWidget' => true,
// default settings for each widget from kartik\widgets used when autoWidget is true
'autoWidgetSettings' => [
Module::FORMAT_DATE => ['type'=>2, 'pluginOptions'=>['autoclose'=>true]], // example
Module::FORMAT_DATETIME => [], // setup if needed
Module::FORMAT_TIME => [], // setup if needed
],
// custom widget settings that will be used to render the date input instead of kartik\widgets,
// this will be used when autoWidget is set to false at module or widget level.
'widgetSettings' => [
Module::FORMAT_DATE => [
'class' => 'yii\jui\DatePicker', // example
'options' => [
'dateFormat' => 'php:d-M-Y',
'options' => ['class'=>'form-control'],
]
]
]
// other settings
]
Here is what is in my view:
<?=$form->field($model, 'dated')->widget(DateControl::classname(), [
'type'=>DateControl::FORMAT_DATE,
'language'=>Yii::$app->language,
'ajaxConversion'=>false,
'options' => [
'pluginOptions' => [
'autoclose' => true
]
]
])?>
What I did is ;
I Enable timezone setting in web.php config fie
'displayTimezone' => 'UTC',
'saveTimezone' => 'UTC',
and set
'ajaxConversion'=>true,
so As Mysql does not convert the date and time datatype data to client timezone; the date will not influenced by the timezone when being displayed.
I am working on the basic template of Yii2. I use the 2amigos yii2-date-picker-widget.
Datepicker is working fine, but it keeps showing me the default language (that is English) instead of Spanish.
As you can see in the code below, the language param is set to 'es':
<?= $form->field($model, 'alta')->widget(
DatePicker::className(), [
'inline' => false,
'clientOptions' => [
'format' => 'yyyy-mm-dd',
'weekStart' => 1,
'todayBtn' => 'linked',
'clearBtn' => true,
'language' => 'es',
'autoclose' => true,
'todayHighlight' => true
]
]);?>
It seems tha the 2amigos Datepicker comes into the proper location whith:
DatePickerLanguageAsset::register($view)->js[] = 'bootstrap-datepicker.' . $this->language . '.min.js';
that means #vendor/bower/bootstrap-datepicker/dist/locales/bootstrap-datepicker.es.min.js, that is an existing file containing my desired spanish location:
!function(a){a.fn.datepicker.dates.es={days:["Domingo","Lunes","Martes","Miércoles","Jueves","Viernes","Sábado","Domingo"],daysShort:["Dom","Lun","Mar","Mié","Jue","Vie","Sáb","Dom"],daysMin:["Do","Lu","Ma","Mi","Ju","Vi","Sa","Do"],months:["Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre"],monthsShort:["Ene","Feb","Mar","Abr","May","Jun","Jul","Ago","Sep","Oct","Nov","Dic"],today:"Hoy",clear:"Borrar",weekStart:1,format:"dd/mm/yyyy"}}(jQuery);
Why this translation is not being applied?
A lot of thanks.
Your translation doesn't work because of wrong syntax, You should move Your language param from 'clientOptions' to top level array:
<?= $form->field($model, 'alta')->widget(
DatePicker::className(), [
'inline' => false,
'language' => 'es',
'clientOptions' => [
'format' => 'yyyy-mm-dd',
'weekStart' => 1,
'todayBtn' => 'linked',
'clearBtn' => true,
'autoclose' => true,
'todayHighlight' => true
]
]);?>