I have such a dilema and headache, I'm working in PHP and Mysql using dates.
In Workbench and phpmyadmin this works fine and returns me some rows
SELECT * FROM reserva WHERE STR_TO_DATE( '2017-04-07 19:00:00' , '%Y-%m-%d %H:%i:%s') BETWEEN fechaini AND fechafin
Where fechafin and fechaini are DATETIME Fields, in php I do this INSERT, it works fine, and I'm filling the DB with this.
public function storeReserva($fechaini) {
$uuid = uniqid('', true);
$stmt = $this->conn->prepare("INSERT INTO reserva(unique_id, fechaini, fechafin) VALUES(?, ? , ? + INTERVAL 2 HOUR)");
$stmt->bind_param("sss", $uuid, $fechaini, $fechaini);
$result = $stmt->execute();
$stmt->close();
// check for successful store
if ($result) {
$stmt = $this->conn->prepare("SELECT * FROM reserva WHERE unique_id = ?");
$stmt->bind_param("s", $uuid);
$stmt->execute();
$reserva = $stmt->get_result()->fetch_assoc();
$stmt->close();
return $reserva;
} else {
return false;
}
}
I want to verify if the new fechaini isnt between in any row of the fields
public function storeReserva2($fechaini) {
$stmt1 = $this->conn->prepare("SELECT * FROM reserva WHERE STR_TO_DATE( '.$fechaini' , '%Y-%m-%d %H:%i:%s') BETWEEN fechaini AND fechafin");
$result1 = $stmt1->execute();
$stmt1->close();
if ($result1 !== null)
{
$uuid = uniqid('', true);
$stmt = $this->conn->prepare("INSERT INTO reserva(unique_id, fechaini, fechafin) VALUES(?, ? , ? + INTERVAL 2 HOUR)");
$stmt->bind_param("sss", $uuid, $fechaini, $fechaini);
$result = $stmt->execute();
$stmt->close();
// Revisa si se hizo la reserva con su id unica
if ($result) {
$stmt3 = $this->conn->prepare("SELECT * FROM reserva WHERE unique_id = ?");
$stmt3->bind_param("s", $uuid);
$stmt3->execute();
$reserva = $stmt3->get_result()->fetch_assoc();
$stmt3->close();
return $reserva;
}
The statement doesnt work, it returns something diferent from null whatever the dates are. I'm guessing i'm using the php variable $fechaini wrong if you can help me with the correct syntax.
PD: I tested with and without the dot ($fechaini) I'm not that kinda dumb (I can't see what the sentente return, i'm using android studio... is there a way to see what the PHP echo or print? I'm not an advanced even not a intermediate php user. )