Reputation: 157
I am making the booking script for rent a car. User will enter two dates at the time of booking, start date and end date, what i need that it checks that the entered dates are between the dates that are already stored in the Db for that specific car, In simple words, if car is booked from 2015-23-08 to 2015-25-08, and user entered the dates between these two its shows an error other wise the order should be placed.
Issue in code: it always shows me "in between" even i entered different values
Code:
include 'connection.php';
if (isset($_GET['id'])) {
// escape variables for security
$fleettype = $_POST['fleettype'];
$fleetname = $_POST['fleetname'];
$fleetclass = $_POST['fleetclass'];
$fleetdescription = $_POST['fleetdescription'];
$fleetprice = $_POST['fleetprice'];
$username = $_POST['username'];
$useremail = $_POST['useremail'];
$userphone = $_POST['userphone'];
$sdate = $_POST['sdate'];
$edate = $_POST['edate'];
$time = $_POST['time'];
$driver = $_POST['driver'];
$useremail = $_POST['useremail'];
$id = preg_replace('#[^0-9]#i', '', $_GET['id']);
$products = mysqli_query($con, "SELECT * FROM fleet WHERE id='$id' LIMIT 1 ");
while ($row = mysqli_fetch_assoc($products))
{
$stime = $row["stime"];
$etime = $row["etime"];
}
$entereddate = DateTime::createFromFormat('Y/m/d', $stime);
$contractDateBegin = DateTime::createFromFormat('Y/m/d', $sdate);
$contractDateEnd = DateTime::createFromFormat('Y/m/d', $edate);
if ($entereddate >= $contractDateBegin && $entereddate <= $contractDateEnd)
{
echo "in between";
}
else {
echo "no";
}
}
Upvotes: 2
Views: 2007
Reputation: 94642
You dont need to wrap the date in single quotes if the date is help in a variable, so change your `createFromFormat statements like so.
Also the else has to have an echo "no"
where you just had a "no"
$entereddate = DateTime::createFromFormat('Y/m/d', $stime);
$contractDateBegin = DateTime::createFromFormat('Y/m/d', $sdate);
$contractDateEnd = DateTime::createFromFormat('Y/m/d', $edate);
if ($entereddate >= $contractDateBegin &&
$entereddate <= $contractDateEnd)
{
echo "in between";
} else {
echo "no";
}
You might get better results doing this as I am pretty sure you cannot compare DateTime objects like that.
$entereddate = strtotime($stime);
$contractDateBegin = strtotime($sdate);
$contractDateEnd = strtotime($edate);
if ($entereddate >= $contractDateBegin &&
$entereddate <= $contractDateEnd)
{
echo "in between";
} else {
echo "no";
}
Upvotes: 3
Reputation: 64657
You can't put single quotes around variables, or it won't interpolate them:
$entereddate = DateTime::createFromFormat('Y/m/d', '$stime');
$contractDateBegin = DateTime::createFromFormat('Y/m/d', '$sdate');
$contractDateEnd = DateTime::createFromFormat('Y/m/d', '$edate');
should just be:
$entereddate = DateTime::createFromFormat('Y/m/d', $stime);
$contractDateBegin = DateTime::createFromFormat('Y/m/d', $sdate);
$contractDateEnd = DateTime::createFromFormat('Y/m/d', $edate);
What is happening is, none of the "dates" you entered are valid - it is seeing if the string $stime
- which could just as easily be the string banana
, is a date. It is not. So $entereddate
is set to false. As is $contractDateBegin
, and $contractDateEnd
.
So then, it checks if false >= false && false <= false
, which it is (false == false
). So it will always echo "in between";
http://sandbox.onlinephpfunctions.com/code/0cb5b5fc82598708fdff66d013d041467bb6fd8a
Upvotes: 2