Skip to content

DateTimeImmutable objects cannot be used as arguments for DateTime faker functions like e.g. dateTimeBetween #947

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

Open
AndreasA opened this issue Feb 12, 2025 · 6 comments

Comments

@AndreasA
Copy link

Summary

DateTime generators do not support passing a DateTimeImmutable object.

I know about the issue #512

However, I also noticed that one cannot pass a DateTimeImmutable object to e.g. dateTimeBetween.

This should be quite an easy fix without breaking changes.

It should be enough to change https://github.com/FakerPHP/Faker/blob/v1.24.1/src/Faker/Core/DateTime.php#L39 to check for DateTimeInterface and the rest should work as it is.

Same still applies to the 2.0 branch.

Versions

Version
PHP 8.4
fakerphp/faker 1.24.1
Copy link

stale bot commented Apr 25, 2025

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 1 week if no further activity occurs. Thank you for your contributions.

@AndreasA
Copy link
Author

Still an issue.

@stale stale bot removed the lifecycle/stale label Apr 30, 2025
@pimjansen
Copy link

Still an issue.

We did not plan to change it here. Just convert it manually when it returns from faker?

@AndreasA
Copy link
Author

AndreasA commented May 1, 2025

@pimjansen
I am not talking about the return values but as function arguments for the DateTime faker. for the return value there should probably be anothe DateTimeImmutable provider as well, to avoid the need to manually convert it (I think there is an issue for that).

It would be quite easy to support DateTime and DateTimeImmutable there using the DateTimeInterface for instanceof checks. Currently it is only possible to use DateTime and string for most of those functions.

See, e.g.
https://github.com/FakerPHP/Faker/blob/v1.24.1/src/Faker/Core/DateTime.php#L39
https://github.com/FakerPHP/Faker/blob/2.0/src/Provider/DateTime.php#L22

or
https://github.com/FakerPHP/Faker/blob/v1.24.1/src/Faker/Core/DateTime.php#L113
https://github.com/FakerPHP/Faker/blob/2.0/src/Provider/DateTime.php#L148

Where the function would work just as well if the instanceof check uses DateTimeInterface which means DateTime and DateTimeImmutable could be used as arguments.

@pimjansen
Copy link

We wont change the behaviour in 1x so just change it before injecting

@AndreasA
Copy link
Author

AndreasA commented May 2, 2025

@pimjansen Which is what I am doing currently. Just a bit annoying .

However, I am fine with it not being changed in v1. But would be nice in v2 at least. Actually as there is a v2 branch already, I did not expect it before v2 anyway.

I only added a reply to avoid the issue being closed because of the stale label:

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 1 week if no further activity occurs. Thank you for your contributions.

However, if it is part of the planned feature list for v2, I guess the issue can be closed with a corresponding info.

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

No branches or pull requests

2 participants