1

So I pondered like 2 hours over the following problem (and in the end could solve it through adding a seemingly very trivial detail...)

I was comparing a bunch of dateranges from the mysql database for overlap with a daterange requested by the user (userinput already sanitized).

I used the following if-statement from this thread: Determine Whether Two Date Ranges Overlap

(StartA <= EndB)  and  (EndA >= StartB)

When this condition was met, the user should have been declined his request due to overlap.

Now, as described in the title, I formatted the dateinput from the user to 'Y-m-d' format. Now, everything was fine except that when the end of the userinputted daterange touched the start of a daterange from the database. Then, the condition failed to discover the overlap. I was really confused and it took me 2 hours before I, out of despair, tried to format to "Y-m-d H:i:s", which is the format in the database as well (with H:i:s being set to 00:00:00) and see what happens.

And all of a sudden everything worked fine Oo Why? Im especially confused because ANY OTHER case was evaluated correctly, only this specific case described above wasn't. I'm pretty certain now that php must automatically do or assume something about the dates when they don't have the exact same format. But I don't know what exactly it does, and I'm really keen to know since it might help me at some point when facing a similar issue.

Thanks for your input!

JSONBUG123
  • 111
  • 10
  • 1
    because 'string1' !== 'string2'? – treyBake Jan 09 '19 at 09:14
  • I suppose when you're comparing `datetime` string with `datetime` value in the db, then its much straight forward. If you're comparing `date` i.e `Y-m-d` against `datetime` then you might get strange result because the data `hour:min:seconds` also are important in the comparison. Its better to compare using the exact format in declared in the table. – Oluwatobi Samuel Omisakin Jan 09 '19 at 09:18
  • @treyBake well yes, but thats true for any other of the cases too, and these cases worked Oo – JSONBUG123 Jan 09 '19 at 09:19
  • 5
    PHP doesn't have a native type to store a date without time so it's mandatory to have one. MySQL has both types, with and without date, and if you're having issues it's because you are using the former thus a time is required. Don't confuse a date that's formatted for display with the actual value. – Álvaro González Jan 09 '19 at 09:19

0 Answers0