yii2-date-range icon indicating copy to clipboard operation
yii2-date-range copied to clipboard

Allow override DateRangeBehavior::dateToTime

Open maxxer opened this issue 2 years ago • 0 comments

Prerequisites

  • [x] I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • [x] The issue still exists against the latest master branch of yii2-date-range.
  • [x] This is not an usage question. I confirm having gone through and read the documentation and demos.
  • [x] This is not a general programming / coding question. (Those should be directed to the webtips Q & A forum).
  • [x] I have attempted to find the simplest possible steps to reproduce the issue.
  • [ ] I have included a failing test as a pull request (Optional).

Steps to reproduce the issue

Given this widget config:

    echo DateRangePicker::widget([
        'name' => 'intervallo',
        'value' => date("01/01/Y") . ' - ' . date("d/m/Y"),
        'options' => [
            'placeholder' => Yii::t('all', '-- Filtra per intervallo di date --'),
            'autocomplete' => 'off',
            'class' => 'form-control',
        ],
        'convertFormat' => true,
        'pluginOptions' => [
            'timePicker' => false,
            'calendarWeeks' => true,
            'locale' => [
                'format' => "d/m/Y",
            ],
        ],
    ]);

and this model:

        $dm = new DynamicModel(['intervallo', 'intervalloStart', 'intervalloEnd']);
        $dm->addRule(['intervallo'], 'required')
            ->attachBehavior("datepick", [
                'class' => \kartik\daterange\DateRangeBehavior::class,
                'attribute' => 'intervallo',
                'dateStartAttribute' => 'intervalloStart',
                'dateEndAttribute' => 'intervalloEnd',
                'dateStartFormat' => 'Y-m-d',
                'dateEndFormat' => 'Y-m-d',
            ]);

certain dates are converted to null. This because with the Italian locale, strtotime fails.

// fails
echo date("Y-m-d", strtotime("17/11/2022"));
// accepted
echo date("Y-m-d", strtotime("17-11-2022"));

Expected behavior and actual behavior

I don't know what could be the best solution. As of right now, I implemented the workaround to replace / with - and strtotime is happy. My first thought was to override dateToTime to be compatible with the current (my!) locale, but that's certainly not a solution valid for everyone.

maxxer avatar Nov 17 '22 16:11 maxxer