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

Date-related rules are not working today #1443

Open
henriquemoody opened this issue Jan 29, 2024 · 2 comments
Open

Date-related rules are not working today #1443

henriquemoody opened this issue Jan 29, 2024 · 2 comments
Assignees
Labels

Comments

@henriquemoody
Copy link
Member

henriquemoody commented Jan 29, 2024

This is probably an issue with PHP, not with the library itself. I can't wrap my head around it:

echo DateTime::createFromFormat('Ym', '202302')->format('Ym');
// Outputs 202303

I will create a commit to let the test pass for now, and I will get back to it whenever I can.


Another similar issue: https://github.com/henriquemoody/Validation/actions/runs/8103265516/job/22147529888

@henriquemoody henriquemoody self-assigned this Jan 29, 2024
@reima
Copy link

reima commented Feb 7, 2024

This is caused by a combination of two non-intuitive behaviors of DateTime::createFromFormat:

  • Missing fields are initialized with the current date/time
  • Out of range values trigger overflow behavior to produce a valid date

This means that DateTime::createFromFormat('Ym', '202302') will use the current day of the month, e.g. the parsed date will be 2023-02-31 on January 31st. The overflow behavior "corrects" this to 2023-03-03.

Prefixing the format string with "!" will set missing fields to sensible, fixed default values (i.e. 1 for the day).

@henriquemoody
Copy link
Member Author

Thank you for such detailed answer, @reima!

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

No branches or pull requests

2 participants