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?
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.
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.
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() .
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.
SELECT * FROM download WHERE expiry_date > CURRENT_TIMESTAMP
or
SELECT * FROM download WHERE expiry_date > NOW()
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With