Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate expiry date on MySQL query

Here is my current mysql table

id  | file        |  expiry_date
---------------------------------------
1   | sample.zip  |  2010-02-03 11:07:03
2   | sample2.zip |  2010-07-13 11:07:03

Query:

SELECT * 
  FROM download 
 WHERE expiry_date` `how to validate here`

I want to validate if expiry_date is expired the file cannot download.

How to do that?

like image 525
kampit Avatar asked Jun 23 '10 16:06

kampit


People also ask

How do I check my expiry date?

Read these codes as MMDDYY, where "MM" refers to the month, "DD" refers to the date, and "YY" refers to the year. This is one of the more common codes that you'll see on food items. For example, "121518" would be read as December 15, 2018.

How do I query a date in MySQL?

In MySQL, use the DATE() function to retrieve the date from a datetime or timestamp value. This function takes only one argument – either an expression which returns a date/datetime/ timestamp value or the name of a timestamp/datetime column.

How do I get Currentdate in MySQL?

Simply use the CURDATE() function to get the current date. The date can be displayed in two different formats: ' YYYY-MM-DD ' if it is used in a string context or YYYYMMDD if it is used in a numeric context. There are two other functions that can be used instead of CURDATE() : CURRENT_DATE and CURRENT_DATE() .

How can I tell when a MySQL table was last modified?

SELECT UPDATE_TIME FROM information_schema. tables WHERE TABLE_SCHEMA = 'yourDatabaseName' AND TABLE_NAME = 'yourTableName'; Let us implement the following query to get the last updated time. mysql> SELECT UPDATE_TIME -> FROM information_schema.


2 Answers

SELECT * FROM download WHERE expiry_date > CURRENT_TIMESTAMP

or

SELECT * FROM download WHERE expiry_date > NOW()
like image 145
John Conde Avatar answered Oct 10 '22 20:10

John Conde


One concern you need to consider is what timezone your expiry_date is stored in vs. the timezone of your mysql server. I have used solutions like the following:

SELECT * FROM `download` WHERE `expiry_date` > NOW();

The solution, however, did not necessarily give me the correct answer I was looking for as the NOW() function is localized to the timezone of the mysql server. Unless your expiry_dates went into the server already localized to your server with NOW(), you'll get an incorrect comparison.

In all of our systems, we store timestamps in the database using UTC. Unfortunately, the data center we host with requires the servers to be localized to EST, which potentially messes up all of our comparisons. The solution was to use the UTC_TIMESTAMP() function, which returns the UTC date and time un-localized.

SELECT * FROM `download` WHERE `expiry_date` > UTC_TIMESTAMP();
like image 38
David L Ernstrom Avatar answered Oct 10 '22 22:10

David L Ernstrom