简体   繁体   中英

How to export a datetime value from mysql to php

I want to do something like a promotion code feature for my ecomm website. Currently I have a variable called $currentDate which I'm not sure is the correct format.

$currentDate = date("Y-M-D") 

I would like to compare that to a date in my database called expiryDate. In my database I have a row with promoCode = GSS2016 (varchar), discount = 0.1 (double, suppose to represent 10% discount) and expiryDate = 2016-07-28 (datetime which is in YMD if I'm not wrong...) I already have run a query to check if the promocode exist but I'm unsure of how to export it into PHP and to compare it with the current date

$currentDate = date("Y-M-D");

session_start();

$conn = mysqli_connect("localhost", "root", "","websiteDb");

$promoCodeInput = $_POST['promoCode'];

$sql = "SELECT * FROM promocodedb WHERE promoCode = '$promoCodeInput'";

$codeChecker = mysqli_query($conn, $sql) or die (mysqli_error($conn));

if (mysqli_num_rows($codeChecker) > 0)
{
    $fetchDiscount = mysqli_fetch_assoc($codeChecker);

    $expiry = $fetchDiscount['expiryDate'];

    if ($expiry > $currentDate)
    {
        $discountPercentage = $fetchDiscount['discount'];
        echo $discountPercentage;

        $cookie_value = $discountPercentage;
        setcookie("discount", $cookie_value, time() + (60 * 60)); //discount last for 1 hour

        header("Location:cart.php?status=promoApplied");
    }
    else
    {
        //header("Location:cart.php?status=promoExpired");  
    }
}
else
{
    header("Location:cart.php?status=promoFail");   
}

YMD produces a string like 2016-Jul-25 . You cannot compare that reliablly against another string, or even a mysql date/time formatted-string, which would be 2016-07-25 .

String comparison rules will NEVER work properly when comparing numbers/dates:

php > var_dump('2016-Jul-25' > '2016-07-25');
bool(true)
php > var_dump('2016-Jul-25' == '2016-07-25');
bool(false)

Even though those two versions represent the same date, you can see the comparisons come back wrong. By string rules, they're NOT the same dates.

As well, note this one:

php > var_dump('2016-Jul-25' > '2016-Mar-25');
bool(false)

True by human parsing standards, because July comes later in the year than March, but FALSE by string comparison standards, because J comes earlier in the alphabet than M , therefore the Jul version is SMALLER/earlier than the Mar version.

You can transfer all datetime into Unix timestamp and compare between each other as timestamps. Unix timestamp is number of seconds since January 1 1970.

The function for transfering datetime into Unix timestamp is strtotime

This is quite clever function and can recognize many datetime formats.

Look at PHP strtotime

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM