Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow override DateRangeBehavior::dateToTime #168

Open
5 of 6 tasks
maxxer opened this issue Nov 17, 2022 · 0 comments
Open
5 of 6 tasks

Allow override DateRangeBehavior::dateToTime #168

maxxer opened this issue Nov 17, 2022 · 0 comments

Comments

@maxxer
Copy link

maxxer commented Nov 17, 2022

Prerequisites

  • I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • The issue still exists against the latest master branch of yii2-date-range.
  • This is not an usage question. I confirm having gone through and read the documentation and demos.
  • This is not a general programming / coding question. (Those should be directed to the webtips Q & A forum).
  • 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant