Fix createFromFormat to respect setTestNow for missing components#494
Open
dereuromark wants to merge 1 commit into3.xfrom
Open
Fix createFromFormat to respect setTestNow for missing components#494dereuromark wants to merge 1 commit into3.xfrom
dereuromark wants to merge 1 commit into3.xfrom
Conversation
When a test time is set via setTestNow(), createFromFormat() now uses values from the test time for any date/time components not present in the format string. This makes testing code that uses partial date formats more reliable and predictable. The fix: - Detects which date/time components are in the format string - For missing components, uses values from testNow instead of real time - Respects '\!' and '|' modifiers which intentionally reset components Closes #452
markstory
reviewed
Feb 7, 2026
Comment on lines
+696
to
+701
| // If the format includes '!' or '|', PHP resets unspecified components to Unix epoch or zero | ||
| // In that case, we should not override with testNow | ||
| $hasReset = in_array('!', $formatChars, true) || in_array('|', $formatChars, true); | ||
| if ($hasReset) { | ||
| return $dateTime; | ||
| } |
Member
There was a problem hiding this comment.
Why are we adding new syntax to PHP's datetime formats?
Member
Author
There was a problem hiding this comment.
We're not adding new syntax - ! and | are existing PHP DateTime format modifiers (see PHP docs).
The code is detecting their presence so we know to skip applying testNow values. When a user explicitly uses ! or |, they're telling PHP to reset unspecified components to Unix epoch, so we should respect that intent rather than substituting testNow values.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When
setTestNow()is used for testing,createFromFormat()now uses values from the test time for any date/time components not present in the format string.Previously, partial formats like
createFromFormat('m-d', '10-05')would use the real system time for the year, making tests unreliable. Now it uses the testNow year.The fix:
!and|modifiers which intentionally reset unspecified components to Unix epoch/zeroExample
Test plan
!and|reset modifiersCloses #452