如何正确比较strtotime的两个值

时间:2018-04-24 00:05:33

标签: php mysql

如果预订时间不到2天,我会阻止用户取消预订。我尝试通过获取当前日期和预订日期来做到这一点,将它们转换为strtotime,然后检查一个值是否大于另一个。但是,当我运行我的代码时,它只是重定向到只显示$ diff

值的页面

这是我的代码

<?php
$customerRef = $_SESSION['customerRef'];
$messageCancelError;
$messageSuccess;
if (isset($_POST['Cancel'])) 
{
    if ($_SERVER['REQUEST_METHOD'] == 'POST') 
    {
        $cancelAppointment = $_POST['cancel'];
        //echo $cancelAppointment;
        $sql4 = "SELECT dateOfBooking FROM booking WHERE bookingRef=? AND customerRef=?";
        // initalise the prepared statement
        $stmt4 = mysqli_stmt_init($conn);
        // prepare the prepared statement
        if (mysqli_stmt_prepare($stmt4, $sql4)) 
        {
            // bind values to the prepared statement
            mysqli_stmt_bind_param($stmt4, "ss", $cancelAppointment, $customerRef);
            // execute the prepared statement
            mysqli_stmt_execute($stmt4);
            // store the results of the prepared statement
            mysqli_stmt_store_result($stmt4);
            // bind the results of the prepared statement to variables
            mysqli_stmt_bind_result($stmt4, $appointmentDate);
            mysqli_stmt_fetch($stmt4);
            //echo mysqli_stmt_error($stmt4);
            //gets the current date in y-m-d format
            $date = date('Y-m-d'); //gets the difference in todays date and the date of an appointment in seconds
            $diff = abs(strtotime($date) - strtotime($appointmentDate)); echo $diff; 
        }
        $stmt4->close();
    }
    $stmt4->close();

    //172800 seconds in 2 days
    if ($diff > 172800) 
    {
        // checking for customer ref as well to prevent a user cancelling another users booking
        $sql3 = "DELETE FROM  booking WHERE bookingRef =? AND customerRef =?";
        $stmt3 = mysqli_stmt_init($conn);
        if (mysqli_stmt_prepare($stmt3, $sql3)) 
        {
            mysqli_stmt_bind_param($stmt3, "si", $cancelAppointment, $customerRef);
            mysqli_stmt_execute($stmt3);
            mysqli_stmt_store_result($stmt3);
            // echo mysqli_stmt_error($stmt3);
        }
        if (mysqli_stmt_affected_rows($stmt3) === 1) 
        {
            $messageSuccess = "Booking Cancelled";
        }
        else 
        {
            $messageCancelError = "Couldn't cancel your booking, check your booking ref against existing ones ";
        }
    }
    // Close statement
    $stmt3->close();
    // Close connection
    // $conn->close();
}

我已尝试将其更改为此但错误仍在发生

   if ($diff > 172800) {

        // checking for customer ref as well to prevent a user cancelling another users booking
        $sql3 = "DELETE FROM  booking WHERE bookingRef =? AND customerRef =?";
        $stmt3 = mysqli_stmt_init($conn);
        if (mysqli_stmt_prepare($stmt3, $sql3)) {
            mysqli_stmt_bind_param($stmt3, "si", $cancelAppointment, $customerRef);
            mysqli_stmt_execute($stmt3);
            mysqli_stmt_store_result($stmt3);

            // echo mysqli_stmt_error($stmt3);

        if (mysqli_stmt_affected_rows($stmt3) === 1) {
            $messageSuccess = "Booking Cancelled";
        }//affected rows
        }//stmt_prepare
        }//$diff
        else {
            $messageCancelError = "Couldn't cancel your booking, check your booking ref against existing ones ";
        }

1 个答案:

答案 0 :(得分:0)

您的if块需要更改。

变化:

//172800 seconds in 2 days
    if ($diff > 172800) 
    {
        ...
        if (mysqli_stmt_affected_rows($stmt3) === 1) 
        {
            $messageSuccess = "Booking Cancelled";
        }
        else 
        {
            $messageCancelError = "Couldn't cancel your booking, check your booking ref against existing ones ";
        }
    }
    // Close statement

要:

//172800 seconds in 2 days
if ($diff > 172800)
{
    ...
    if (mysqli_stmt_affected_rows($stmt3) === 1)
    {
        $messageSuccess = "Booking Cancelled";
    }
}
else
{
    $messageCancelError = "Couldn't cancel your booking, check your booking ref against existing ones ";
}
// Close statement