How to check expiration date in 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 check if expiry_date expired, the file cannot be downloaded.

How to do it?

+4
source share
2 answers
 SELECT * FROM download WHERE expiry_date > CURRENT_TIMESTAMP 

or

 SELECT * FROM download WHERE expiry_date > NOW() 
+8
source

One issue you should consider is the amount of time your expiry_date is stored in the time zone of your mysql server. I used the following solutions:

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

The solution, however, did not necessarily give me the correct answer I was looking for since the NOW () function is localized in the time zone of the mysql server. If your expiry_dates did not get to a server already localized on your server using NOW (), you will get an incorrect comparison.

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

 SELECT * FROM `download` WHERE `expiry_date` > UTC_TIMESTAMP(); 
+1
source

Source: https://habr.com/ru/post/1313674/


All Articles