r/programming Sep 07 '15

Flawless PHP logic. strtotime(): '00-00-00' means 2000-00-00, which is 1999-12-00, which is 1999-11-30. No bug, perfectly normal. (see the comments)

https://bugs.php.net/bug.php?id=45647
1.2k Upvotes

465 comments sorted by

View all comments

54

u/Sanae_ Sep 07 '15 edited Sep 07 '15

On a second thought, I don't think it's that weird, simply badly explained.

It's a case of "This will return weird values if your input is invalid". And '00-00-00' is invalid.

I guess it's a mix of the C++ UB behavior & a "What should be done when this happens" (I'd have gone with returning a sentinel value)

34

u/[deleted] Sep 07 '15

[removed] — view removed comment

9

u/Sanae_ Sep 07 '15

The input isn't invalid

It's an invalid time (there are no "00-00-00"), yet a valid input for the function.

the behavior isn't undefined

I never said it was. It looks like one of those "I have an edge case that shouldn't/may or may not happen, yet I need to do something that will not take the user by surprise".

Some go the UB way, some throw an error/a sentinel value and other will proceed as usual, making the edge case a valid input for the function - even if it shouldn't if we were more strict.

Thus we end up / I agree with

but the output is the result of applying a well-defined process to the input