php - 如何正确比较 strtotime 中的两个值

标签 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 ";
        }

最佳答案

您的 if block 需要更改。

更改:

//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

关于php - 如何正确比较 strtotime 中的两个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49991588/

相关文章:

php - file_put_contents 在 cron 作业中无法正常工作

php - 通过社交网络数据库结构登录

mysql - 如何每小时访问不同机器的mysql表数据?

php - 在 PHP 中为数据类型创建 'IF' 语句

PHP-MySQL | PHP-MySQL那么更好/更干净的方式吗?

php - mysql 结果检索列名和值

php - 通过运行 cron 来更改访问的代码

javascript - 使用 html2canvas 的屏幕截图需要在文件夹中另存为 psd

mysql - 如何在 MySQL 中设计一个 "view"来获得我正在寻找的结果?

java - mybitis运行更新sql错误