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

When using date fields without time the `is_between`modifier returns false on same day #2157

Closed
efoken opened this Issue Sep 3, 2018 · 0 comments

Comments

Projects
None yet
2 participants
@efoken

efoken commented Sep 3, 2018

Describe the bug
When using a date field, the is_between modifier returns false on same day, which makes sense, because the date string are converted to Carbon objects with time – which is midnight by default.

But I think this behaviour is wrong – if one selects validity dates (without time) in CP, he assumes that the selected dates are included in the comparison.

To Reproduce
Steps to reproduce the behavior:

  1. Create a fieldset with valid_to and valid_from date fields with no time allowed
  2. Create a page using those fields
  3. Make a comparison like {{ if now | is_between:valid_from:valid_to }} in template
  4. See that valid_to date is not included in range

Expected behavior
The valid_to date should be included in the is_between comparison, so the second date should get
23:59:59 added if there is no time in the string.

Solution could be something like:

public function isBetween($value, $params, $context)
{
    $date1 = carbon(array_get($context, $params[0], $params[0]));
    $date2 = array_get($context, $params[1], $params[1]);

    if (is_string($date2) && preg_match('/^\d{4}-\d{2}-\d{2}$/', $date2)) {
        $date2 .= '23:59:59';
    }

    return carbon($value)->between($date1, carbon($date2));
}

Environment details (please complete the following information):

  • Statamic Version 2.10.4
  • Fresh Install or Upgrade: Upgrade
  • PHP Version: 7.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment