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

DateTime::createFromFormat with now throws an error: Call to a member function setTime() on bool #22

Closed
faille76 opened this issue Aug 5, 2022 · 0 comments · Fixed by #23

Comments

@faille76
Copy link
Contributor

faille76 commented Aug 5, 2022

Hello,

I'm having an issue when I want to instantiate a DateTime using the createFromFormat method if I specify the format as now and the datetime as a date with no time (ex: 2022-08-04).

Example:

ClockMock::freeze(new \DateTimeImmutable('1986-06-05 12:13:14'));
$dateTime = \DateTime::createFromFormat('\n\o\w', '2022-01-01');

Output:

Error: Call to a member function setTime() on bool

Expected:
The createFromFormat method must return false if the date doesn't match the format.

Solution:
Do not try to set the time when \DateTime::createFromFormat returns false.
In src/ClockMock.php:
Replace

private static function mock_date_create_from_format(): callable
{
// ...
if ($parsedDate['hour'] === false) {
// ..
}

With:

private static function mock_date_create_from_format(): callable
{
// ...
if ($parsedDate['hour'] === false && $dateTimeObject !== false) {
// ...
}

~ faille76

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

Successfully merging a pull request may close this issue.

1 participant