Answer the question
In order to leave comments, you need to log in
What is the best way to handle date with datepicker in yii2?
Question exclusively on yii2.
yii\jui\DatePicker returns the date in the format dd.yyyy.MM. It is stored in the int field in the database. And of course it is necessary to convert the date.
What is the best way to do this, given that the form should work correctly.
- If the date is already selected, the field should be filled with the correct value
- An int value should be written to the database if the date field is filled and cleared if it was made empty
----------
Now I decide like this.
/**
* @property integer $some_time - поле в базе
*/
class News extends ActiveRecord {
public function rules() {
return ;
}
public function getSomeTimeDate() {
return $this->some_time? date('d.m.Y', $this->some_time) : '';
}
public function setSomeTimeDate($date) {
$this->some_time= $date ? strtotime($date) : null;
}
}
...
echo $form->field($model, 'someTimeDate')->widget('common\widgets\Datepicker', [
'dateFormat' => 'dd.mm.yy',
])
Answer the question
In order to leave comments, you need to log in
You will need to configure the validation www.yiiframework.com/doc-2.0/guide-tutorial-core-v... specify the format in which the date will come from the browser (format=>'php:dYm') and specify the name of the attribute in the database (timestampAttribute )
The topic was considered for a long time, but suddenly it will come in handy for someone, he himself faced such a task. Alternatively
Create a DateToTimeBehavior
<?php
namespace common\behaviors;
use yii\behaviors\AttributeBehavior;
use yii\base\InvalidConfigException;
class DateToTimeBehavior extends AttributeBehavior {
public $timeAttribute;
public function getValue($event) {
if (empty($this->timeAttribute)) {
throw new InvalidConfigException('Can`t find "fromAttribute" property in ' . $this->owner->className());
}
if (!empty($this->owner->{$this->attributes[$event->name]})) {
$this->owner->{$this->timeAttribute} = strtotime($this->owner->{$this->attributes[$event->name]});
return date('d.m.Y', $this->owner->{$this->timeAttribute});
} else {
if (!empty($this->owner->{$this->timeAttribute})) {
$this->owner->{$this->attributes[$event->name]} = date('d.m.Y', $this->owner->{$this->timeAttribute});
return $this->owner->{$this->attributes[$event->name]};
}
}
return date('d.m.Y', time());
}
}
... ... ...
use common\behaviors\DateToTimeBehavior;
... ... ...
public $date;
... ... ...
... ... ...
public function behaviors() {
return [
[
'class' => DateToTimeBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_VALIDATE => 'date',
ActiveRecord::EVENT_AFTER_FIND => 'date',
],
'timeAttribute' => 'time', //Атрибут модели в котором хранится время в int
],
];
}
... ... ...
use kartik\date\DatePicker;
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'date')->widget(
DatePicker::className()
)?>
<?php ActiveForm::end(); ?>
in the view
if(!empty($model->end_at)){
$model->end_at = date('d.m.Y H:i', $model->end_at);
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['start_at', 'end_at'], 'filter', 'filter' => function ($value) {
if(!preg_match("/^[\d\+]+$/",$value) && $value > 0){
return strtotime($value);
}
else{
return $value;
}
}],
];
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question