I already googled and thereby also searched stackoverflow a lot on this, but the more I searched the more "?" popped up in my face and I must admit that I feel like I can't put all these things together on my own, so I hope I can articulate myself well enough here so that you can hopefully help me out in this...^^
So, I want to compare dateranges inputted by a user to dateranges stored in my MYSQL DB in the format "2019-02-02 00:00:00". Yes, all the dates in the DB are set to midnight.
The daterange coming from the front-end was formatted on the php side to resemble this structure. Therefore, I took the timestamp from the front-end and put it into the date() function:
date("Y-m-d 00:00:00", $timestamp)
Now, later on I'm a SQL query with which I determine whether there is overlap between the two dateranges or not, it looks like this:
$result = $connection->query("SELECT id
FROM reservierung
WHERE status != 'abgelehnt'
AND benutzer != $benutzerID
AND arbeitsplatz = $arbeitsplatzID
AND $formattedStartDate <= ende
AND $formattedEndDate >= anfang
");
I then evaluate the query, like this:
$queryResult = $result->rowCount();
But php throws an error because the query has failed an returns the boolean false. Therefore I conclude that something must have gone wrong with the datecomparison, because if I remove it, the query succeeds.
I know that MYSQL only stores strings (at least our DB definitely does), but I dont know what my date is (a string? an object?) and I don't know what I need to convert into what to make this comparison happen xD I usually always extracted the dates and made my comparisons outside the queries, but this is a lot more "verbose" and probably unnecessary if one knows how to compare inside mysql. I read about DATE_FORMAT() already and that comparing timestamps instead of dates would work too, but I dont know which method would be more efficient (in terms of producing less and better structured code) and again, I don't know what needs to be what inside the mysql query to make the comparison work on a mere functional/syntactical level...^^
EDIT: Here is the php error, just FYI:
[Thu Feb 07 15:29:04.739350 2019] [:error] [pid 20986] [client 127.0.0.1:47518] PHP Fatal error: Uncaught Error: Call to a member function rowCount() on boolean in /var/www/html/include/Buchen.php:76\nStack trace:\n#0 /var/www/html/include/Buchen.php(199): checkQueryResult(false)\n#1 /var/www/html/include/Buchen.php(149): reservationConflictsWithAlreadyExistingReservationByUser(Object(PDO), '2019-02-07', '2019-02-28', '66', '145', Array)\n#2 /var/www/html/include/Buchen.php(24): checkForValidityOfReservationTimeframe(Object(PDO), Array, 'D.E.I.3', '2', '66', '145')\n#3 /var/www/html/include/Buchen.php(11): mainframe('1549494000', '1551308400', '2', 'D.E.I.3', 'Andreas', 'Lang', NULL)\n#4 {main}\n thrown in /var/www/html/include/Buchen.php on line 76, referer: http://localhost/view/reservieren.php