Search code examples
phpmysqlprepared-statementstatements

MySQL select where timenow > database time


I have a PHP script which creates a new order and inserts the time NOW + 15 minutes into the database.

After 15 minutes have expired the user cannot access the order page.

On the admin panel I only want to show records that have not expired, so my question is what select statement would I use to do this, and which would be the most efficent?

SELECT * FROM `orders` WHERE datenow > date

The "date" is the date of the order + 15 minutes

P.S. Aternativly, is there any way to do this using PHP so I don't have to put too much stress on the database?

Many Thanks!


Solution

  • Given that you want records that have not yet expired, you want all records where date is greater than NOW().

    SELECT 
        * 
    FROM 
        `orders` 
    WHERE 
        `date` > NOW()
    

    As the comments mentioned, it is usually better to handle this on the database side; rather than transfer (assuming you have split database and web servers) the entire data set to filter it in the application code.

    The other benefit of having all the datetime functions be on the same server (in the database in this case), is that especially on some shared hosting environments, the timezone for the database cannot be changed. Therefore, you want the time comparisons to occur in the same timezone.