简体   繁体   中英

Comparing PHP current time to a mysql datetime column

I have searched and all questions are related to the comparison being in the query. I'm building an e-commerce website and I have a feature that allows the customer to return products 7 days upon date of purchase.

So I have, a column in mysql that tells when they bought. And I have to compare the current time (Philippines) to the mysql column, so the system can tell if they will have the option allowed.

The logic I'm thinking of but can't find the right syntax:

(inside the while loop that fetches columns as arrays, my variable is $info)
$php_current_datetime = date_now_in_php_format;
$purchase_datetime = $info['purchase_date'];
$expire_date = $info['purchase_date'] + 168 hours;

Then compare it

if($php_current_datetime < $expire_date){
echo "show the option";
}
else{
echo "already expired";
 }

I would recommend making all the values timestamps and calculate the difference between them in seconds.

$secDifference = 60*60*168;

$dtObject= new DateTime($mysqlDatetime);
if ($dtObject->getTimestamp() > time()-$secDifference) {
    //show the option
}

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