php / doc-es

Spanish translation of the PHP documentation
24 stars 50 forks source link

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

Closed jotajimenezc closed 2 years ago

jotajimenezc commented 2 years ago

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 commented 2 years ago

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

jotajimenezc commented 2 years ago

Its a default value for SQL databases, but When you try to set a value -0001 you cant insert it

El vie., 10 dic. 2021 8:22, Damian Wadley @.***> escribió:

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

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/php/doc-es/issues/23#issuecomment-990678762, or unsubscribe https://github.com/notifications/unsubscribe-auth/AW3WGKILMTBBQ2JE4NLBP5TUQGTEHANCNFSM5JYNW3NQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

damianwadley commented 2 years ago

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.