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

Fail to create new datetime with value '0000-00-00' #23

Closed
jotajimenezc opened this issue Dec 10, 2021 · 3 comments
Closed

Fail to create new datetime with value '0000-00-00' #23

jotajimenezc opened this issue Dec 10, 2021 · 3 comments
Labels
invalid This doesn't seem right

Comments

@jotajimenezc
Copy link

From manual page: https://php.net/class.datetime

when we create a new variable with value '0000-00-00' the variable get a wrong value

DateTime Object
(
[date] => -0001-11-30 00:00:00.000000
[timezone_type] => 3
[timezone] => Europe/Berlin
)

@damianwadley
Copy link
Member

Do you think 0000-00-00 is a valid date?

@damianwadley damianwadley added the invalid This doesn't seem right label Dec 10, 2021
@jotajimenezc
Copy link
Author

jotajimenezc commented Dec 11, 2021 via email

@damianwadley
Copy link
Member

0000-00-00 is not a valid date, but PHP will do its best to transform it into a valid date. The result is -0001-11-30.
MySQL only supports dates from 1000-01-01 to 9999-12-31, so it will not support year -0001 (and also not support year 0001).

If you want to use DateTime then you need to make sure the date is not 0000-00-00 first. You'll need to think about what your code should do if it receives that value: what does it mean, and can you use it as a real date, and how should you display it to a user?

Or instead, I suggest you should use NULL and not the default date. Then your PHP values will always be either nulls or valid DateTimes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

2 participants