Validation icon indicating copy to clipboard operation
Validation copied to clipboard

Date-related rules are not working today

Open henriquemoody opened this issue 2 years ago • 2 comments

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 avatar Jan 29 '24 21:01 henriquemoody

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).

reima avatar Feb 07 '24 00:02 reima

Thank you for such detailed answer, @reima!

henriquemoody avatar Feb 09 '24 13:02 henriquemoody

Hello @henriquemoody !

Could you please provide status on this issue? I believe in 2.3.7 bug were still present?

Thanks, Anna

ayurenia-seedbox avatar May 30 '24 13:05 ayurenia-seedbox